
Think together · Start with a little story
The robot sees the same function name appear inside a function. It is not erasing the old task and starting over; it is beginning a new, smaller task.
Lay out the process
- First callHandle n = 2
- Second callHandle n = 1
- Third callHandle n = 0
- ReturnFinish the waiting tasks in turn
A function can call itself, and each call has its own arguments and execution position.
Stack three task cards labeled n = 2, 1, and 0 to see that these calls can be at different stages at the same time.
01Can a function really call itself?
Yes. Here is a simple example:
void countdown(int n) {
if (n == 0) {
return;
}
std::cout << n << '\n';
countdown(n - 1);
}
When countdown(3) runs, it calls countdown(2), which then calls countdown(1).
02Does the same function “start over”?
No.
Every call creates a new function-call state. The calls run the same function code, but their argument values and local variables can differ.
n = 3
n = 2
n = 1
03Why does one call not overwrite the previous call’s n?
Each active function call has its own arguments and local state.
While countdown(3) is still running, countdown(2) begins as a new call. The earlier call’s n = 3 still belongs to that earlier call.
04Why can’t a function call itself forever?
If a function keeps calling itself without a way to stop, the number of active calls keeps growing.
void bad() {
bad();
}
This function has no path to termination. In a real program it will usually fail when it runs out of resources.
05Is a self-call the same as a loop?
Loop
Control flow returns to the same loop structure for another iteration.
Recursive call
A new function call is created, and it returns after deeper calls finish.
Both can express repetition, but their execution models are different.
06Why learn the call stack before going deeper into recursion?
The easiest part of recursion to misunderstand is not the code, but that several calls can exist at once.
Once you can see who called whom, which call is active, and where it returns, recursion stops looking like a template to memorize.
What have you learned?
- A function can call itself.
- Every self-call is a new function call.
- Different calls can have different arguments and local state.
- A self-call does not “restart in place” or copy the function code.
- Without a stopping mechanism, recursive calls keep adding more levels.
Next: What Is Recursion?
Your turn to try
The innermost call changes n to 0. Does the outer card labeled n = 2 also change to 0?
Ready? Open to see the explanation
No. When arguments are passed by value, each call has its own argument. The outer call still remembers its own value, 2.