
Think it through together · A little story
A girl asks a robot to make lunch. Halfway through, the robot goes to boil water. When it is done, the robot needs to remember which step of lunch-making to return to.
Lay out the process
- Main taskPrepare lunch
- SubtaskBoil water
- Subtask endsReturn
- Main taskContinue preparing lunch
When one function calls another, the caller waits for that call to finish, then continues.
Imagine writing “what to do when you come back” on a task card. That helps explain why the return point must be remembered.
01Can one function call another?
Of course:
int doubleValue(int x) {
return x * 2;
}
int addDouble(int a, int b) {
return doubleValue(a) + doubleValue(b);
}
While running, addDouble calls doubleValue.
02Where does control flow go?
A function call is like stepping away temporarily: first finish the called function, then come back and continue from where you were.
03How does the program know where to continue after returning?
A function call needs to save information such as “where to continue after this call finishes.”
On common machines and ABIs, calling conventions, registers, the call stack, and other mechanisms work together to manage this information.
04Are the arguments the same in every call?
int a = doubleValue(3);
int b = doubleValue(10);
Both calls run the same function code, but they receive different argument values and therefore produce different results.
05Can two calls to the same function overwrite each other?
Each function call has its own call state for ordinary local parameters and local variables.
x = 3
x = 10
It is not “the same x being changed to two values at once.”
06Do function calls have a cost?
Calls usually involve some overhead for calling, passing arguments, and returning. Modern compilers can sometimes use inlining to remove or transform a function call.
What you have learned
- One function can call another function.
- During a call, control flow temporarily enters the called function.
- When that function finishes, execution continues after the call site.
- Each call has its own arguments and local state.
- The underlying mechanism often involves a return point, registers, and the call stack, but the details depend on the platform.
Next: What is the call stack?
Your turn to try
When the water-boiling function returns, does the lunch function start again from its first line?
Have you thought it through? Open for an explanation
No. It usually continues from the point after the call. The saved return point tells the program which step to resume.