1 / 9

Scope of Variables

Scope of Variables. Lecture 13. Scope of Variables. The "scope" of a variable is the region within a program where that variable name is defined.

kiaria
Download Presentation

Scope of Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scope of Variables Lecture 13 Winter Quarter

  2. Scope of Variables • The "scope" of a variable is the region within a program where that variable name is defined. • A variable that is declared before any statements which define a function (such as "void main ( )" ) has filescope. That is, its name, its designated memory location, and any value assigned to it will be known throughout the main function and any other function (subprogram) in the same file of source code. Winter Quarter

  3. Scope of Variables • A variable that is declared immediately after the statements " void main { " will have the main program as its scope but will not be known outside the main function (for instance, not in function scanf). This is known as block scope. • A variable that is declared inside a block of statements (inside or after the "{" ) will have only that block as its scope. It's scope ends at the "}". • It is less confusing when all declarations either come before any functions (global variables) or only after the first "{" in a function (local variables). Winter Quarter

  4. Scope of Variables • The same variable name can not be defined (declared) twice within the same block of code. A block of code is the code inside of a set of braces { }. However, a variable in C++ can be declared in other blocks of code (and even within nested blocks). • If the same variable name is declared in multiple nested blocks of code, the declaration that is within the particular block of code that is being executed at that time is the one that is currently "in scope" or in effect. Winter Quarter

  5. Scope of Variables Example • The following program will not compile because x is only defined inside the if block of statements and is not known outside that block (neither before nor after it). Winter Quarter

  6. Scope of Variables /* This program doesn't compile, x is not declared */ #include <stdio.h> int main ( ) { FILE *fptr ; fptr = fopen("scope.dat", "w") ; fprintf(fptr, ”before if block, x=%d\n", x); if ( x == 3 ) /* x not declared yet -- error */ { float x = 4.5; fprintf(fptr, "in if block, x=%f\n", x); } fprintf( fptr, "after if block, x=%d\n", x); } Winter Quarter

  7. Scope of Variables #include <stdio.h> int main ( ) {int x = 3 ; /* Now "x" known, program compiles */ FILE *fptr ; fptr = fopen("scope.dat", "w") ; fprintf(fptr, ”before if block, x=%d\n", x) ; if ( x == 3 ) { float x = 4.5 ; fprintf(fptr, "inside if block, x=%f\n", x) ; } fprintf(fptr, "after if block, x=%d\n", x) ; } Winter Quarter

  8. Scope of Variables • When the program is modified as was shown on the previous slide, x is known throughout the program, but the x inside the if block of statements has a different data type and has a different value than the one outside the block. • These two versions of the variable, x, have two different memory locations, and thus we have two completely different variables with the same name. (Confusing, eh?) Winter Quarter

  9. Scope of Variables • As proof, let's look at the output file from the program, which was written to "scope.dat": before if block, x=3 inside if block, x=4.500000 after if block, x=3 • Note that when the if block is in scope, x is a floating point variable whose value is 4.5, but outside the if block it is an integer variable with a value of 3. Winter Quarter

More Related