Variable Scope
A variable cannot be used everywhere in a program. The part of the program where you can use a variable's name is called its scope. The rule in C++ is short: a variable exists from the line where you declare it to the closing } of the block it is in.
Local variables
A variable declared inside a function is a local variable. It belongs to that function only. Two functions can each have a variable with the same name, and they are two separate variables:
#include <iostream>
using namespace std;
void showBonus() {
int bonus = 10;
cout << "showBonus: " << bonus << "\n";
}
int main() {
int bonus = 99;
showBonus();
cout << "main: " << bonus << "\n";
return 0;
}
Output:
showBonus: 10
main: 99
Setting bonus to 10 in showBonus did not touch the bonus in main. This is a good thing. You can choose names inside a function without checking every other function first. To share a value between functions, pass it as a parameter or send it back with return.
Block scope
Every pair of braces { } makes a block, for example the body of an if or a loop. A variable declared inside a block is gone after its closing }:
#include <iostream>
#include <string>
using namespace std;
int main() {
int score = 75;
if (score >= 50) {
string result = "pass";
}
cout << result << "\n";
return 0;
}
Output:
main.cpp: In function ‘int main()’:
main.cpp:10:13: error: ‘result’ was not declared in this scope
result was declared on line 8, inside the if block. On line 10 the block has ended, so the name does not exist any more. The fix is to declare the variable before the block, and only give it a value inside:
int score = 75;
string result = "fail";
if (score >= 50) {
result = "pass";
}
cout << result << "\n";
Output:
pass
The counter of a for loop follows the same rule. i in for (int i = 0; ...) exists only inside the loop.
Global variables
A variable declared outside every function is a global variable. Every function in the file can read it and change it:
#include <iostream>
using namespace std;
int total = 0;
void addPoints(int points) {
total += points;
}
int main() {
addPoints(10);
addPoints(5);
cout << total << "\n";
return 0;
}
Output:
15
This looks handy, but try to avoid it. When any function can change total, a wrong value can come from anywhere, and the bug is hard to find. Parameters and return values show clearly what goes into a function and what comes out.
Shadowing
If you declare a variable with the same name as one outside the block, the inner one shadows (hides) the outer one until the block ends. This often happens by mistake, when you write a type in front of a name that already exists:
int best = 0;
int score = 80;
if (score > best) {
int best = score;
}
cout << best << "\n";
Output:
0
int best = score; made a new best inside the if block. That new variable got 80 and was gone at the }. The outer best is still 0. The program compiled with no error. To change the outer variable, write best = score; without the type.
In the exercise, a program has two scope mistakes. Find them and fix them.