Seeing Further with Variable Scope - C++

All code that you write in a program can fit one of four categories, each with its own rules about which variables can access it:

  • Code inside the main function
  • Code in another function
  • Code in a block of code, such as an if statement or a namespace
  • Code outside functions

Within each of these categories are rules for how long a variable lasts (variable lifetime) and where it can be accessed (variable scope).
The general rules for variable scope and lifetime are as follows:

  • If the variable is declared outside a block of code, it is called global, and its scope and lifetime are from the point of declaration until the end of the source file. (As we mentioned earlier in this chapter, a block of code is any code within curly braces.)
  • If the variable is declared within a block of code, it is called local, and its scope and lifetime are until the end of the block.
  • If the variable is declared within an argument list of a function, it is called a parameter, and its scope and lifetime are until the end of the function.

All this means that functions cannot access the variables declared within the main function unless they are passed as arguments to the function.
If a variable is declared within a block that is within another block, the scope of the variable is restricted to the inner block.
Perhaps this example will help clarify the preceding rules:

Output:

The value of main’s a is: 5 The value of main’s b is: 3 The value of global is: 5 The value of subtract’s a is: 5 The value of main’s a now is: 5 the value of global now is: 9

In line 5, the global variable, global, is declared. In line 11, main’s two local variables, a and b, are declared. In line 17, the values in main’s a and b are copied into subtract’s a and b. subtract then finds the difference and assigns it to a in line 27. When this value is returned from the subtract function, global = 2 + subtract(a,b), you have the following:

global = 2 + 7;

In other words, because subtract returns the value of subtract’s a, line 17 is just adding 2 to the returned value, which is 7 in this case.
Remember that two separate, unrelated versions of a and b are in this code; main’s a has nothing to do with subtract’s a.

Specifying with the Scope Resolution Operator

Sometimes you might come across situations where a global variable and a local variable have the same name. This is legal in C++, but it can cause some confusion. Here is an example:

It is hard to tell at first glance which version of intVar is assigned the value 5, but a rule does exist. If a local variable has the same name as a global variable, the local variable is said to shadow the global variable. All operations performed on a variable by that name are performed on the local variable. How then do you access the global variable in these situations?
The answer is by using the scope resolution operator. If you want to make sure that the global version of the variable is used, just put two colons (::) in front of it. For example, to make the preceding code use the global version, you change the previous code to the following:

This shadowing effect can also happen with subblocks. For example, if you have an if statement within the main function, a variable declared within the if block can shadow a global variable or a local variable of main. However, the scope resolution operator can be used only to specify a global variable. For example, if an if statement variable shadows a main variable, you can access the main version of that variable. Here’s an illustration:

Output:

Elves often live in the woods.—-Humans and elves can coexist.

Using Static Variables

You will sometimes find it convenient to use the same variable within a function every time the function is called. Local variables don’t work because they are re-created every time the function is called. Global variables will work fine, except that sometimes they can be inconvenient. One solution is to use static variables.
Static variables have the same scope as normal variables, but their lifetime lasts until the end of the program. In this way, you can create a function that has a “memory.”
You declare a static variable with the static keyword:

The statement in which the static variable is created is executed only once for the whole program. This means that if you initialize the static variable on the same line

this initialization will be executed only once.
Take a look at the following example:

Output:

incrementing both variables value of function1 is: 1 value of function2 is: 1 incrementing both variables value of function1 is: 2 value of function2 is: 1 incrementing both variables value of function1 is: 3 value of function2 is: 1 incrementing both variables value of function1 is: 4 value of function2 is: 1

In this example, both x and y are local variables. While x is static and is initialized only once, y is initialized to 0 every time incrementFunction2 is called. For this reason, incrementFunction2 always returns 1, whereas incrementFunction1 will continue to increment x.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C++ Topics