C++ functions: Local, Global, and Static Local Variables

A variable can be declared as a local, a global, or a static local in C++.

As mentioned in Section 2.5, “Variables,” the scope of a variable is the part of the program where the variable can be referenced. A variable defined inside a function is referred to as a local variable. C++ also allows you to use global variables. They are declared outside all functions and are accessible to all functions in their scope. Local variables do not have default values, but global variables are defaulted to zero.

A variable must be declared before it can be used. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. The scope of a global variable starts from its declaration and continues to the end of the program.

A parameter is actually a local variable. The scope of a function parameter covers the entire function.

Listing 6.11 demonstrates the scope of local and global variables.

Listing 6.11 VariableScopeDemo.cpp

1 #include <iostream>
2
using namespace std;
3
4
void t1(); // Function prototype
5 void t2(); // Function prototype
6
7
int main()
8 {
9     t1();
10    t2();
11
12   
return 0;
13 }
14
15
int y; // Global variable, default to 0
16
17
void t1()
18 {
19   
int x = 1;
20    cout <<
“x is ” << x << endl;
21    cout <<
“y is ” << y << endl;
22    x++;
23    y++;
24 }
25
26
void t2()
27 {
28   
int x = 1;
29    cout <<
“x is ” << x << endl;
30    cout <<
“y is ” << y << endl;
31 }

A global variable y is declared in line 15 with default value 0. This variable is accessible in functions t1 and t2, but not in the main function, because the main function is declared before y is declared.

When the main function invokes t1() in line 9, the global variable is incremented (line 23) and becomes 1 in t1. When the main function invokes t2() in line 10, the global variable y is now 1 .

A local variable x is declared in t1 in line 19 and another is declared in t2 in line 28. Although they are named the same, these two variables are independent. So, incrementing x in t1 does not affect the variable x defined in t2.

If a function has a local variable with the same name as a global variable, only the local variable can be seen from the function.

1. The Scope of Variables in a for Loop

A variable declared in the initial-action part of a for-loop header has its scope in the entire loop. However, a variable declared inside a for-loop body has its scope limited in the loop body from its declaration to the end of the block that contains the variable, as shown in Figure 6.4.

Figure 6.4 A variable declared in the initial action part of a for-loop header has its scope

in the entire loop.

It is commonly acceptable to declare a local variable with the same name in different nonnesting blocks in a function, as shown in Figure 6.5a, but it is not a good practice to declare a local variable twice in nested blocks, even though it is allowed in C++, as shown in Figure 6.5b. In this case, i is declared in the function block and also in the for loop. The program can compile and run, but it is easy to make mistakes. Therefore, you should avoid declaring the same variable in nested blocks.

2. Static Local Variables

After a function completes its execution, all its local variables are destroyed. These variables automatic variable are also known as automatic variables. Sometimes it is desirable to retain the values stored in local variables so that they can be used in the next call. C++ allows you to declare static local static local variable variables. Static local variables are permanently allocated in the memory for the lifetime of the program. To declare a static variable, use the keyword static.

Listing 6.12 demonstrates using static local variables.

Listing 6.12 StaticVariableDemo.cpp

1 #include <iostream>
2
using namespace std;
3
4
void t1(); // Function prototype
5
6
int main()
7 {
8     t1();
9     t1();
10
11   
return 0;
12 }
13
14
void t1()
15 {
16   
static int x = 1;
17   
int y = 1;
18    x++;
19    y++;
20    cout <<
“x is ” << x << endl;
21    cout <<
“y is ” << y << endl;
22 }

A static local variable x is declared in line 16 with initial value 1. The initialization of static variables happens only once in the first call. When t1() is invoked for the first time in line 8, static variable x is initialized to 1 (line 16). x is incremented to 2 (line 18). Since x is a static local variable, x is retained in memory after this call. When t1() is invoked again in line 9, x is 2 and is incremented to 3 (line 18).

A local variable y is declared in line 17 with initial value 1. When t1() is invoked for the first time in line 8, y is incremented to 2 (line 19). Since y is a local variable, it is destroyed after this call. When t1() is invoked again in line 9, y is initialized to 1 and is incremented to 2 (line 19).

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *