< Previous | Contents | Next >

Accessing Global Variables

You can access a global variable from anywhere in your program. To prove it, I display glob in main() with:

cout << "In main() glob is: " << glob << "\n\n";

The program displays 10 because as a global variable, glob is available to any part of the program. To show this again, I next call access_global(), and the computer executes the following code in that function:

cout << "In access_global() glob is: " << glob << "\n\n";

Again, 10 is displayed. That makes sense because Im displaying the exact same variable in each function.

Using Global Variables 169