Blocks and Variable Scope

The scope of a variable is that set of code statements in which the variable is known to the compiler; where is can be referenced in your program. Most commonly, the scope of a variable is limited to the code block in which it is defined. A code block is a set of code enclosed in braces ({, }). Although C/C++ allows us to create a code block almost anywhere we desire, the most common code blocks are functions. int Difference (int a, int b) { int diff; diff = a - b; return diff; } In this function, the scope of the variable diff is between the braces of the function Difference(). diff is said to be a local variable. Note that the arguments (a and b are also considered local variables for the function. Other variables named diff, a, b that are declared outside of the function Difference() are distinct from these local variables.)

The ability to declare local variables in a function allows the function to be a completely self-contained unit of code.

Other common code blocks include the bodies of if, while, do-while, for and case statements.

Recall that in C++ variables may be declared anywhere within a code block. A good programming practice is to limit the scope of all variables to whatever extent possible. This means that you should NOT declare all variables at the beginning of a block (as you were required to do using C), but rather should declare variables only when needed.

One confusing (and hence poor) programming practice is to reuse variable names in nested blocks. What is the output from this code?

#include <iostream> using namespace std; int main ( ) { const int MAXAGE = 42; int age = 23; int bobsAge = 33; if (age < MAXAGE) { int age = 320; if (bobsAge < age) { for (int age = 0; age < MAXAGE; age++) { cout << "Happy Birthday\n"; } cout << "You are " << age << " years old\n"; } } cout << "I am " << age << " years old\n"; return 0; }

One application of this principle allowed in the C++ language is the ability to define variables in the heading of a for loop. Most commonly the loop index is declared and initialized in the for loop heading. These variables are considered local to the for loop.

for (int i = 0; i < size; i++) { array[ i ] = 42; if (i < 10 ) cout << "Happy Birthday" << endl; } // using 'i' here generates a compiler error

Global Variables

Any variable that is defined outside the body of all functions including outside of main( ) is said to have global scope and can be used by any code that follows the variable declaration.

The use of globabl variables is strictly forbidden in this course.

Global named constants (eg const float PI = 3.14159;) are permitted. However, your program should follow the principle of least scope and define global constants only when they are truly global and no other variable declaration is sufficient.

An Exercise

Self-Test Exercise 25 on page 126 (shown below) is a good test of your understanding of scope. You should determine the output of the code as written, then change each occurrence of the variable x to be a distinct variable. Self-Test Exercise 26, page 126 This example exhibits poor programming practices, but provides a good exercise to help you understand variable scope rules. State the output that this code fragment would produced if embedded in an otherwise complete, correct program. { int x = 1; cout << x << endl; { cout << x << endl; int x = 2; cout << x << endl; { cout << x << endl; int x = 3; cout << x << endl; } cout << x << endl; } cout << x << endl; }

Last Modified: Monday, 28-Aug-2006 10:16:00 EDT