Presentation is loading. Please wait.

Presentation is loading. Please wait.

Local, Global Variables, and Scope

Similar presentations


Presentation on theme: "Local, Global Variables, and Scope"— Presentation transcript:

1 Local, Global Variables, and Scope

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 Scope The scope of a declaration is the block of code where
Some code enclosed in braces The scope of a declaration is the block of code where the identifier is valid for use. 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. 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) It is possible to declare the same identifier name in different parts of the program: local to a block

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

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 Local to a block ? void f() { int x; x=1; int a; a=2;
cout << a << endl; cout << x << endl; } ?

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

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 Block scope is local within a pair of braces {…}
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 Global variables int x; int main() { x=0;
cout << x << endl; int b; b=1; { int c; c=2; cout << c << endl; }

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

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

13 Identifier name and conflict resolution
An identifier can be defined only once in the same scope We can not have two variables/constants of the same name even they have different types 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 Example 1 Number in Increment() is the global variable.
#include <iostream> 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 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 Example 3 int Number; //global variable void Increment(int Number) { Number = Number + 1; cout << Number << endl; } void main() { Number = 1; Increment(Number); 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 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 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; Two(A,B,C); void main() { A = 1; B = 2; C = 3; D = 4; One(A,B,C);

19 Output:

20 Scope resolution example
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 of y (global) scope of f scope of z scope of a (local to while-block) scope of s & t scope of r scope of i

21 Summary Pass by reference Pass by value Global variable
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); 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; 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;

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

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


Download ppt "Local, Global Variables, and Scope"

Similar presentations


Ads by Google