Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.

Similar presentations


Presentation on theme: "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables."— Presentation transcript:

1 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables Lecture 13

2 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 2Winter Quarter 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 file scope. 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.

3 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 3Winter Quarter 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).

4 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 4Winter Quarter 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.

5 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 5Winter Quarter 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).

6 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 6Winter Quarter Scope of Variables /* This program doesn't compile, x is not declared */ #include void 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); }

7 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 7Winter Quarter Scope of Variables #include void 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) ; }

8 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 8Winter Quarter 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?)

9 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 9Winter Quarter 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.


Download ppt "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables."

Similar presentations


Ads by Google