Presentation is loading. Please wait.

Presentation is loading. Please wait.

Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.

Similar presentations


Presentation on theme: "Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double."— Presentation transcript:

1 Local, Global Variables, and Scope

2 COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double a,b,c; … } int two(bool a, …) { char I,j,k; … } void f1(int a, …) { double x,y,z; … } void f2(double a, …) { bool x,y,z; }

3 COMP104 Slide 3 Scope The scope of a declaration is the block of code where the identifier is valid for use. n A global declaration is made outside the bodies of all functions and outside the main program. It is normally grouped with the other global declarations and placed at the beginning of the program file. n A local declaration is one that is made inside the body of a function. Locally declared variables cannot be accessed outside of the function they were declared in. Local to a function (the variables in Main are also local, local to ‘main’ function) n It is possible to declare the same identifier name in different parts of the program : local to a block Some code enclosed in braces

4 COMP104 Slide 4 Local variables to a function int main() { int x,y,z; … } void f() { int x; … }

5 COMP104 Slide 5 Formal parameters are local to a function * All variables/constants in the formal parameters and inside the body are local to the function * They can not be used by others * They are short-lived: they come when the function is called, and go when the function returns.

6 COMP104 Slide 6 Local to a block void f() { int x; x=1; { int a; a=2; cout << a << endl; cout << x << endl; } cout << a << endl; cout << x << endl; } ?

7 COMP104 Slide 7 Always, local first void f() { int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; } ?

8 COMP104 Slide 8 In a for-loop { int i; for (i=1;i<10;i++) cout << A[i]; } for (int i=1;i<10;i++) cout << A[i]; equivalent

9 COMP104 Slide 9 Block scope is local * within a pair of braces {…} n For, while, do-while, if, else, switch, etc … * All variables/constants in the block are local to the block * They can not be used out of the block * They are short-lived as well: they come when the block is entered, and go when the block is finished.

10 COMP104 Slide 10 Global variables int x; int main() { x=0; cout << x << endl; int b; b=1; { int c; c=2; cout << c << endl; } cout << c << endl; }

11 COMP104 Slide 11 Global is ‘file’ scope l Global variables are initialized to 0 when not explicitly initialized l Global variable can be accessed by anyone in the same file l When a global variable is used in a different file, it needs to have a ‘external declaration’ l Functions are all ‘Global’ l When a function is used in a different file, it needs to be re-declared

12 COMP104 Slide 12 Who’s who? int x; int main() { x=0; cout << x << endl; int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; }

13 COMP104 Slide 13 Identifier name and conflict resolution * An identifier can be defined only once in the same scope n We can not have two variables/constants of the same name even they have different types n Wrong: int x=1; double x=1.0; * But, the same identifier can be ‘re-used’ in different scopes. * When an identifier is declared more than once, that identifier in the innermost scope is selected by the compiler.  local first!!!

14 COMP104 Slide 14 Example 1  Number in Increment () is the global variable. #include using namespace std; int Number; //global variable void Increment(int Num) { Num = Num + 1; cout << Num << endl; Number = Number + 1; } void main() { Number = 1; Increment(Number); cout << Number << endl; }

15 COMP104 Slide 15 Example 2 int Number; //global variable void Increment(int& Num) { Num = Num + 1; cout << Num << endl; Number = Number + 1; } void main() { Number = 1; Increment(Number); cout << Number << endl; } * When Increment is called, Num refers to global variable Number * Number = Number + 1 also refers to global variable Number.

16 COMP104 Slide 16 Example 3 Number int Number; //global variable void Increment(int Number) { Number = Number + 1; cout << Number << endl; } void main() { Number Number = 1; Number Increment(Number); cout << Number << endl; } * The scope of the global variable Number does not include Increment(), because Increment() already has a local parameter of the same name. * Thus, the changes made to Number are lost when control returns to the main program.

17 COMP104 Slide 17 Global Variables * Undisciplined use of global variables may lead to confusion and debugging difficulties. * Instead of using global variables in functions, try passing local variables by reference.

18 COMP104 Slide 18 Example 4 int A,B,C,D; void Two(int A, int B, int& D) { B = 21; D = 23; cout <<A<< " " <<B<< " " <<C<< " " <<D<< endl; } void One(int A, int B, int& C) { int D; // Local variable A = 10; B = 11; C = 12; D = 13; cout <<A<< " " <<B<< " " <<C<< " " <<D<< endl; Two(A,B,C); } void main() { A = 1; B = 2; C = 3; D = 4; One(A,B,C); cout <<A<< " " <<B<< " " <<C<< " " <<D<< endl; Two(A,B,C); cout <<A<< " " <<B<< " " <<C<< " " <<D<< endl; }

19 COMP104 Slide 19 * Output: 10 11 12 13 10 21 23 23 1 2 23 4 1 21 23 23 1 2 23 4

20 COMP104 Slide 20 int y = 38; void f(int, int); void main( ){ int z=47; while(z<400){ int a = 90; z += a++; z++; } y = 2 * z; f(1, 2); } void f(int s, int t){ int r = 12; s = r + t; int i = 27; s += i; } Scope resolution example scope of i scope of r scope of s & t scope of a (local to while-block) scope of z scope of y (global) scope of f

21 COMP104 Slide 21 int MIN; void min(int,int); int main() { int x,y; cin >> x >> y >> endl; min(x,y); cout << MIN; } void min(int a, int b) { if (a<b) MIN=a; else MIN=b; } void min(int,int,int&); int main() { int x,y,mini; cin >> x >> y >> endl; min(x,y,mini); cout << mini; } void min(int a, int b, int& m) { if (a<b) m=a; else m=b; } int min(int,int); int main() { int x,y,mini; cin >> x >> y >> endl; mini=min(x,y); cout << mini; } int min(int a, int b) { int m; if (a<b) m=a; else m=b; return (m); } Summary Global variable Pass by referencePass by value

22 COMP104 Slide 22 int x; int main() { x=0; cout << x << endl; int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; } Summary global local (to the main) local (to the block)

23 COMP104 Slide 23 n Global vs. local n Function scope n Block scope n Global variables (forbidden!!!) n Everything is relative  File scope!


Download ppt "Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double."

Similar presentations


Ads by Google