Scope

Earlier I remarked that variables declared within blocks are not defined outside that block. It would be hard to do serious programming if this were not true; you would be forced to come up with a unique name for every variable. The part of a program in which a variable is visible is called that variable's scope.

Global Variables

Variables declared outside functions are called global because they are visible from any function in the system; such a variable has global scope. Side effects are a problem in large programs, where it is hard to know exactly where a variable was changed. Unlike in some languages, C++ global variables are automatically available to all functions defined in a file (this is called file scope).

Local Variables

Declarations within a block—that is, local variables—are visible only inside that block. Local variables have local scope and will hide any variables that are declared outside the block. That is, the local definition always dominates. The formal arguments (or parameters) of the function are considered to be declared as local variables. In the following example, the variable var is redefined within scope_test().


;> int var = 1;
;> void scope_test(int a, int b) {
;1>  int var = a + b;
;1>  cout << a << " " << b << " var = " << var << endl;
;1>  a = 999;
;1> }
;> int a = 11;
;> scope_test(a,22);
11 22 var = 33
;> var;
(int) 1
;> a;
(int) 11

In this example, there is a variable var, which has global scope, and a local variable var declared within the function. However, global var and local var are completely independent variables; they don't have to be the same type, and they won't interfere with each other. Likewise, the global variable a and the local variable a share only a name; you can modify the local a as much as you like, without modifying the global a.

It helps to know how names are looked up in C++. First the local scope is examined and then any enclosing local scopes. Only then does C++ examine global scope. If you really need to access the hidden variable var, you can use the global scope operator (::), as in the following example:

;> void another_test() {  int var = 0; cout << ::var << endl; }
;> another_test();
1

Recall that uninitialized variables are not guaranteed to have a 'sensible' value. Global variables will initially be zero, but local variables initially have arbitrary values. This is a common source of problems, and it causes bugs that mysteriously come and go. As much as possible, you should initialize local variables and generally keep the declaration as close to the first use of the variable as possible. This example shows a typical symptom of an uninitialized local variable:

void show_var() {
 int ii;
 cout << ii << endl;
}
;> show_var();
7521584

It is important to realize that local variables are by default volatile, which means they do not keep their values after the function has finished executing. (Actually, they may randomly keep their values, which is even worse than not keeping their values.) This may appear to be a strange default (and some languages don't do it), but it is a consequence of how C++ allocates local variables. Later you will see why this is the case, (in the section “Recursion” in Chapter 6), but for now, don't depend on local variables to remember anything.

Note that a variable might not be in scope but still be alive and well. For instance, the following example depends on the second function not forgetting the value of ii while the first function is called:

void second(int I) {  cout << I << endl; }
void first() {
  Int ii = 2;
  Second(ii);
  Cout << ii << endl;
}

If you want a local variable that is not volatile, you can use the static qualifier with a variable declaration. This kind of variable is initialized once, at the start of the program, and it keeps its value until the next call of the function. In this example, the function show_it() has a local variable ii. But because it has been declared as static, this variable will remember its value:


void show_it() {
 static int ii = 0;
 ii++;
 cout << ii << endl;
 }
;> show_it();
1
;> show_it();
2
;> show_it();
3

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.171.107