Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
The declaration of bonus appears inside addBonus, so code in that function can use the name after its declaration. The return expression uses bonus to compute the result, and the assignment inside the if statement changes that same local variable. The name does not become available to every function in the program just because addBonus can be called from main.
int addBonus(int score) {
int bonus = 5;
if (score >= 10) {
int boost = 2;
bonus = bonus + boost;
}
return score + bonus;
}
int main() {
int score = 10;
cout << addBonus(score) << " " << addBonus(score);
// cout << bonus; // invalid: bonus is not in main's scope
}The scope of bonus starts at its declaration and continues through the closing brace of addBonus. An expression such as score + bonus can therefore compile inside that function. An expression such as cout << bonus in main is different: main is outside those braces, so the compiler cannot find a variable with that name. This is a compile-time error, not an unpredictable value produced while the program runs.
Where can an expression using bonus compile in the running program?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The if statement creates a code block inside addBonus. The declaration of boost is inside that nested block, so boost can be used by the assignment before the block's closing brace. After that brace, the name boost is no longer available. The name bonus has a wider scope, because its declaration is in addBonus rather than inside the if block, so the return expression can still use bonus.
int addBonus(int score) {
int bonus = 5;
if (score >= 10) {
int boost = 2;
bonus = bonus + boost;
} // boost's scope ends here
return score + bonus; // bonus is still in scope
} // bonus's scope ends hereWhen main evaluates addBonus(score) with score equal to 10, that call creates its own parameter score, its own bonus initialized to 5, and its own boost initialized to 2. The condition is true, so bonus becomes 7, and the call returns 10 + 7, which is 17. When the return finishes, those locals stop existing with that call.
The second call does not reopen the first call's variables. It creates another parameter score holding 10, another bonus initialized to 5, and another boost initialized to 2. It again changes bonus from 5 to 7 and returns 17. The two returned values are therefore 17 17, not 17 19.
What output does main print after calling addBonus(score) twice with score equal to 10?
cout << addBonus(score) << " " << addBonus(score);Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The score in main and the score parameter in addBonus have the same spelling, but they are separate variables in separate scopes. The value of main's score is used as the argument for each call. Each call then gives that value to its own parameter score. Changing a variable inside addBonus selects that call's local variable, not main's variable merely because the names match.
int main() {
int score = 10;
cout << addBonus(score) << " " << addBonus(score);
}
int addBonus(int score) {
int bonus = 5;
if (score >= 10) {
int boost = 2;
bonus = bonus + boost;
}
return score + bonus;
}The assignment bonus = bonus + boost changes only the bonus created by the current call. It does not change main's score, and it does not change a bonus from an earlier call. Local names describe variables only within the scopes that contain them, so identical spelling is not shared storage and does not extend a variable's lifetime.