Recall that each variable in modern programming languages has:
(Recall that variables are being created and destroyed while the program is running)
|
So space used to stored the variable need to be reclaimed (variable is then destroyed)
Therefore, a variable has a lifetime
Lifetime has to do with when a variable is created and destroyed
Scoping has to do with when a variable is accessible and used.
 
 
 
In other words, which parts of your program can see or use the variable.
|
The collection of all variables that are visible/accessing at a given point is called the program scope
Every variable NAME that is defined in the outer scope, will be visible (i.e.: known) in the inner scope |
We will learn the precise scoping rule shortly.
And you can define an "inner inner scope" inside the inner scopex, and so on (scoping levels can go on indefinitely) |
|
..... { (Outer scope) .... .... { (Begin of inner scope) (Inner scope) .... .... } (End of inner scope) .... .... } |
int main(int argc, char *argv[]) { int x; x = 1; // int x is accessible after its definition cout << "x = " << x << "\n"; // prints 1 { x = 3.14; // int x is ALSO inside new scope cout << "x = " << x << "\n"; // prints 3 } cout << "x = " << x << "\n"; // prints 3 } |
int main(int argc, char *argv[]) { int x; x = 1; // int x is accessible after its definition cout << "x = " << x << "\n"; // prints 1 { float x; <-------------- change x = 3.14; // int x is ALSO inside new scope cout << "x = " << x << "\n"; // prints ??? } cout << "x = " << x << "\n"; // prints ??? } |
We say that:
|
int main(int argc, char *argv[]) { { float x; <-------------- x = 3.14; // int x is ALSO inside new scope cout << "x = " << x << "\n"; // prints 3.14 } cout << "x = " << x << "\n"; // prints ??? } |
 
 
 
|
A variable exits
only
locally inside the scope
in which it is defined
For this reason, variables defined inside a scope are called local variables Furthermore, a local variable can be accessed only after its definition |
int main(int argc, char *argv[]) { ... CANNOT access any variable int var1; <--------- var1 starts to exist .. var1 accessible { .. var1 accessible float var2; <--- var2 starts to exist var1 and var2 are accessible } <--- var2 ends to exist .. only var1 accessible... } <--------- var1 ends to exist float f(float x) { ... CANNOT access any variable int var3; <--------- var3 starts to exist .. var3 accessible { .. var3 accessible float var4; <--- var4 starts to exist var3 and var4 are accessible } <--- var4 ends to exist .. only var3 accessible... } <--------- var3 ends to exist |
int x; int main(int argc, char **argv) { .... } float f(float x) { .... } |
{ int x; int main(int argc, char **argv) { .... } float f(float x) { .... } } |
|
|
Java will forbid the user to define another variable with the same name in an inner scope
Java adopts a "careful" approach, and tries to prevent the programmer from "confusing oneself"... put it bluntly: Java assumes that you don't know what you're doing...
Java is made for beginning programmers who do not know what they are doing.... But if you know what you're doing, you will like C++ better.