Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.

Similar presentations


Presentation on theme: "Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends."— Presentation transcript:

1 Chapter 8 Scope of variables Name reuse

2 Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends primarily on where you declare it There are also keywords you can use when declaring a variable that affect its scope

3 Variable Scope Global – can be referenced within any function in the program Function – can be referenced anywhere within a function Block – can be referenced within a block (curly braces {})

4 Global Scope Variables defined outside of functions are global A global variable can be used by any function in the file that is defined after the global variable It is best to avoid programmer-defined global variables Exceptions tend to be important constants Globals with appropriate declarations can even be used in other program files

5 Global Example const float pi = 3.14159; // usual use of global int i = 2; // example of global scope int main() { int inMain = 3; cout << i << endl; // print 2 cout << pi << endl; // print 3.14159 funca(inMain); return 0; } void funca(int passed) { cout << passed * i; // print 6 cout << pi * i; // print 6.28318 }

6 Function Scope Parameters passed to a function and any variables declared at the start of the function have function scope Function scope means that they can be referenced anywhere in the function (including all blocks within the function)

7 Function Scope Example int main(int argc, char *argv[]){ int inMain = 3; // Function scope cout << inMain << endl; // print 3 funca(inMain); cout << inFunca; // illegal return 0; } void funca(int passedParm) { // func scope int inFunca = 4; // Function scope cout << inFunca << endl; // Prints 4 cout << inMain << endl; // illegal cout << passedParm << endl; //prints 3 }

8 Blocks can be put anywhere a statement can be put Blocks within blocks are nested blocks Typical use of blocks are for while, for, if clauses A variable is known only within the block in which it is defined and in nested blocks of that block Block Scope A block is a list of statements within curly braces

9 Block Scope Example void funca(int passedParm) { function scope int inFunca = 4; // Function scope if (passedParm == 3) { int inIf = 5; // Block scope cout << inFunca <<endl; // Print 4 cout << inIf <<endl; // Print 5 } else { cout << inFunca <<endl;// Print 4 cout<< inIf; // illegal } cout << inIf; //illegal }

10 Name Reuse If a nested block defines a variable with the same name as enclosing block, the new definition is in effect in the nested block Each scope area can have a variable of the same name: Global (one variable only) Function (each function) Block (each block)

11 Name Reuse Example string s = "global"; // global i known everywhere int main () { string sreturn; string s = "main"; // this s is known within main cout << "s in main = " << s << endl; sreturn = func1(s); cout << "func1 returned " << sreturn << endl; sreturn = func2(s); cout << "s from func2 in main = " << sreturn << endl; return 0; }

12 string func1 (string local) { cout << "func1 got " << local << " from caller" << endl; cout << "s in func1 (gets global s) = " << s << endl; string s = "func1"; // now declare our own s s = func2(s); cout << "s from func2 in func1 = " << s << endl; return s; }

13 string func2 (string local) { if (local == "main") { string s = "if true"; return (s); } else { string s = "if false"; return (s); }

14 Using a File in a Function The file identifier (handle) is used by the operating system to keep track of the file (for example, what to read next) If you want to use a file in a function: Pass the file ID by reference, or Declare the file ID as a global (above main)

15 File Use in a Function Pass the file ID by reference: void readFile(int& custID, ifstream& myFile) { myFile >> custID; return; } OR make myFile a global variable


Download ppt "Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends."

Similar presentations


Ads by Google