local, global variables, and scope. comp104 slide 2 functions are ‘global’, variables are...
Post on 21-Dec-2015
232 views
TRANSCRIPT
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 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;
}
COMP104 Slide 3
Scope
The scope of a declaration is the block of code wherethe 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
Some code enclosed in braces
COMP104 Slide 4
Local variables to a function
int main(){ int x,y,z; …}
void f(){
int x; …}
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.
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;}
?
COMP104 Slide 7
Always, local first
void f(){ int x; x=1;
{int x;
x=2;cout << x << endl;
} cout << x << endl;}
?
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
COMP104 Slide 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.
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;}
COMP104 Slide 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
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;}
COMP104 Slide 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!!!
COMP104 Slide 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; }
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.
COMP104 Slide 16
Example 3 int NumberNumber; //global variable
void Increment(int Number) { Number = Number + 1; cout << Number << endl; } void main() { NumberNumber = 1; Increment(NumberNumber); 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.
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.
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;}
COMP104 Slide 19
Output:
10 11 12 1310 21 23 231 2 23 41 21 23 231 2 23 4
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 rscope of s & t
scope of a
(local to while-block)
scope of z
scope of y
(global)scope of f
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 reference Pass by value
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)
COMP104 Slide 23
Global vs. local Function scope Block scope
Global variables (forbidden!!!)
Everything is relative File scope!