C++ Scope
The overloading page ended with forty-six lines on the screen, and the last one was 6, the result of scale(count, 3). This page starts from that screen, in the same file types.cpp, and the first box still holds the word pine. The announce, show_label, and scale functions stay above main. This page continues the Functions module. The new work adds a small function that creates its own local box. That box is visible only inside the function braces. After the call, main still prints its own count. The screen grows to forty-eight lines, and the last one is 2.
Scope is the region of the file where a name can be used. A name declared inside a function body belongs to that function. A name declared inside main belongs to main. The braces mark the border. Outside those braces, that local name does not exist for the compiler. Reading a name is always a question of which braces you are standing in, not a question of whether the spelling appeared somewhere else in the file earlier.
Programs rely on scope so two functions can both have a box named n without fighting. Parameters already showed a local name filled by the caller. This page adds a local name that the function creates itself, then shows that main still uses count afterward. The workshop loops and the scale calls stay above the new lines, so the new 9 and the final 2 sit clearly at the end of the screen where you can count them.
Above main, after the scale overloads, sits void mark_local(). Inside its braces, int local = 9; creates a whole-number box that lives only for that function call. The next line prints it: std::cout << local << std::endl;. When the function ends, that local box is gone.
Inside main, after the scale prints, the file calls mark_local();, then prints count with std::cout << count << std::endl;. The call prints 9 from inside mark_local. The next line prints 2, because count in main still holds 2. Nothing in mark_local changed count.
Walk the new ending. The lines through 4 and 6 from scale stay. Then mark_local(); prints 9. Then count prints 2. The last two new lines are 9, then 2. There are forty-eight lines in all, and the last one is 2. The function did not borrow count, and main did not borrow local. Each scope kept its own boxes for the whole run.
If main tried to print local after the call, the build would fail, because local is not a name in main. That failed experiment stays in the prose, not in the checked file. The checked file only prints names that exist in the current scope: local inside mark_local, and count inside main.
A parameter such as label in show_label is also local to that function. A loop counter such as k inside the early-exit for is local to that for statement’s braces. Scope stacks: inner braces can hide names, but this page keeps one clear local inside one function so the border is obvious.
A common mistake is to assume every name in the file is usable everywhere. Another common mistake is to reuse a local name in main and think it is the same box as the one inside the function. Same spelling in two scopes means two different boxes. A third mistake is to forget that the local box is created again on every call; a second mark_local(); would print 9 again from a fresh local, not from a leftover value sitting in main.
This page stops before the next tool where a function calls itself. The next page names that tool once at the end. Scope still matters there, because each call gets its own locals while the call stack grows.
Type the file in this order, or add only the new lines if types.cpp is already on disk. Above int main(), keep the earlier functions, and add mark_local with int local = 9; and the print of local. Near the end of main, after the scale prints and before return 0;, add mark_local(); and the print of count. The file name on disk stays types.cpp. The checked file is below, and the new lines are the mark_local definition and those two lines in main.
#include <iostream>
#include <string>
void announce()
{
std::cout << "call" << std::endl;
}
void show_label(std::string label)
{
std::cout << label << std::endl;
}
int scale(int n)
{
return n * 2;
}
int scale(int n, int factor)
{
return n * factor;
}
void mark_local()
{
int local = 9;
std::cout << local << std::endl;
}
int main()
{
// the word stays pine
std::string word = "pine";
int count = 2;
double ratio = 0.5;
bool ready = true;
double price = 7.9;
const int limit = 10;
int scores[3] = {4, 5, 6};
std::string other = "cone";
std::string joined = word + " " + other;
int total = 17;
int step = 3;
step += 4;
int score = 72;
int goal = 50;
bool wet = false;
std::string result;
int day = 3;
std::string name;
std::cout << word << std::endl;
std::cout << count << std::endl;
std::cout << ratio << std::endl;
std::cout << ready << std::endl;
std::cout << word.size() << std::endl;
std::cout << joined << std::endl;
std::cout << word[0] << std::endl;
std::cout << joined.find("cone") << std::endl;
std::cout << joined.substr(5, 4) << std::endl;
std::cout << count * 4 << std::endl;
std::cout << 7 / 2 << std::endl;
std::cout << 7.0 / 2 << std::endl;
std::cout << (count > 1) << std::endl;
std::cout << (count == 3) << std::endl;
std::cout << !ready << std::endl;
std::cout << static_cast<int>(price) << std::endl;
std::cout << static_cast<double>(count) / 4 << std::endl;
std::cout << static_cast<int>(ratio * 3) << std::endl;
std::cout << limit << std::endl;
std::cout << limit * count << std::endl;
std::cout << scores[0] << std::endl;
std::cout << scores[2] << std::endl;
std::cout << total % 5 << std::endl;
std::cout << step << std::endl;
std::cout << (score >= goal) << std::endl;
std::cout << (score < goal) << std::endl;
std::cout << ((score >= goal) && (count == 2)) << std::endl;
std::cout << ((score < goal) || wet) << std::endl;
if (score >= goal) {
result = "pass";
} else {
result = "retry";
}
std::cout << result << std::endl;
if (wet) {
std::cout << "wet" << std::endl;
}
switch (day) {
case 1:
name = "mon";
break;
case 2:
name = "tue";
break;
case 3:
name = "wed";
break;
default:
name = "other";
break;
}
std::cout << name << std::endl;
for (int i = 0; i < 3; i++) {
std::cout << i << std::endl;
}
int n = 3;
while (n > 0) {
std::cout << n << std::endl;
n--;
}
for (int k = 0; k < 5; k++) {
if (k == 2) {
continue;
}
if (k == 4) {
break;
}
std::cout << k << std::endl;
}
for (int value : scores) {
std::cout << value << std::endl;
}
announce();
show_label("tag");
int scaled = scale(count);
std::cout << scaled << std::endl;
std::cout << scale(count, 3) << std::endl;
mark_local();
std::cout << count << std::endl;
return 0;
}
Rebuild with g++ -std=c++17 types.cpp -o types, then run ./types. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 types.cpp -o types. The screen shows forty-eight lines. The lines through 6 match the overloading page. The last two lines are 9, then 2.
Check the last two lines first. mark_local printed its local 9. Then main printed count as 2. The table gives each row its piece, so compare your run with it line by line. If the last line is 9, the print of count is missing. If the build fails about local in main, you tried to use the function’s local name outside its braces. If mark_local is missing, the new 9 never appears.
📊 The local scope and what it shows
| Piece | What the screen shows |
|---|---|
| First | call, because announce still runs first among the function calls |
| Last | 2, because main prints count after mark_local returns |
| Rule | a local name is visible only inside the braces where it is declared |
| Screen | pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, 3.5, 1, 0, 0, 7, 0.5, 1, 10, 20, 4, 6, 2, 7, 1, 0, 1, 0, pass, wed, 0, 1, 2, 3, 2, 1, 0, 1, 3, 4, 5, 6, call, tag, 4, 6, 9, 2 |
Try one change at a time. Change int local = 9; to int local = 8;, leave the call as it is, and rebuild. The line that was 9 becomes 8, and the last line stays 2, because only the local box changed. Put 9 back. Next, change the final print from count to scaled, leave mark_local as it is, and rebuild. The last line becomes 4, because scaled still holds the earlier one-argument scale result. Put count back. Each experiment changes one thing, so the screen shows exactly which scope’s name you printed. Undo each change before the next one starts, so the file always returns to the checked version.
This page ends the scope tool and stops before the next one. The next page is recursion, which is a function that calls itself with a smaller problem. The mark_local you have just written only shows where names live. You leave with forty-eight lines on the screen, and the last one is 2. Count the lines before you leave. There are forty-eight, and the last one is 2.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top