Scope and Visibility
In spite of its name, this subject has nothing to do with telescopes or astronomy. C++ has some rules for the naming of variables and their range of accessibility.
Here is a simple example:
#include <iostream>
#include <string>
using namespace std;
void myFunct()
{
string beta = "This is beta.";
cout << beta << endl;
}
int main()
{
myFunct();
cout << beta << endl;
return 0;
}
In this uncompilable example, the compiler will complain that it cannot access the variable named "beta" from the function "main()." The reason? The variable "beta" is "local" to the function "myFunct()". In fact, it only exists in the blue region — it is created there, it is used there, and it is destroyed as the function exits.
If I had placed "beta" outside both functions --
#include <iostream>
#include <string>
using namespace std;
string beta = "This is beta.";
void myFunct()
{
cout << beta << endl;
}
int main()
{
myFunct();
cout << beta << " too" << endl;
return 0;
}
— all functions would have access to "beta". Now "beta" is said to be a "global variable." Global variables are not a very good idea — it is better to define variables in such a way that they are accessible only to the code that needs access to them, no more.
Here is a somewhat more complex example — note the colors:
#include <iostream>
#include <string>
using namespace std;
void myFunct()
{
string beta = "This is beta.";
cout << beta << endl;
for(int i = 0;i < 3;i++) {
string beta = "This is another, different beta.";
cout << beta << endl;
}
cout << beta << endl;
}
int main()
{
myFunct();
return 0;
}
The colors are meant to show that one "beta" has a scope "outside" (if you will) the region of the for-loop's braces. The declaration of another string named "beta" "inside" the for-loop's braces is entirely independent of the same-name variable that is outside. Notice that the green "beta" doesn't exist any more after the for-loop exits.
But if I change the line inside the for-loop like this --
beta = "This isn't another, different beta.";
— I have changed the meaning from a declaration of a new variable with the same name (string beta = ) to assignment to the existing, external variable (beta = ). This form assigns a new value to the existing, external "beta," and the program's behavior changes.
Here is another example:
#include <iostream>
#include <string>
using namespace std;
void myFunct()
{
for(int i = 0;i < 3;i++) {
string beta = "This is another, different beta.";
cout << beta << endl;
}
cout << beta << endl;
}
int main()
{
myFunct();
return 0;
}
In this uncompilable example, the compiler will complain that it cannot find a variable with the name "beta" at the red line. This is because the variable named "beta" inside the for-loop is gone, has been destroyed, no longer exists, after the for-loop exits.
Which leads to this rule:
In general, the scope of a variable is limited to the region within the nearest enclosing braces. Outside that region, the variable does not exist.
The exceptions are (1) if a same-name variable is declared within a subsidiary structure (like our for-loop above), this "inside" variable will temporarily hide the original variable, or (2) a global variable is declared, one that has no enclosing braces.
As has been pointed out, it is best to avoid global variables if possible. Why? Well, you might eventually write large, complex programs, programs that are too large to keep track of every use of a common variable name. Then you will create a variable with a name you've used before, and that other variable will no longer have the effect you hoped for. This is just one example, there are many. Avoid global variables if possible.