Unformatted text preview:

Scope of VariablesSlide 2Slide 3Slide 4Scope of Variables ExampleSlide 6Slide 7Slide 8Slide 9Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 13 P. 1Winter QuarterScope of VariablesLecture 13Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 13 P. 2Winter QuarterScope 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 "int 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.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 13 P. 3Winter QuarterScope of Variables•A variable that is declared immediately after the statements " int 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. Its 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).Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 13 P. 4Winter QuarterScope 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). (In the latter case, a disaster waiting to happen…)• 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.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 13 P. 5Winter QuarterScope 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).Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 13 P. 6Winter QuarterScope 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);}Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 13 P. 7Winter QuarterScope 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) ;}Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 13 P. 8Winter QuarterScope 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?)Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 13 P. 9Winter QuarterScope of Variables•As proof, let's look at the output file from the program, which was written to "scope.dat":before if block, x=3inside if block, x=4.500000after 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


View Full Document

OSU ENGR H192 - Scope of Variables

Documents in this Course
Strings

Strings

22 pages

Load more
Download Scope of Variables
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Scope of Variables and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Scope of Variables 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?