The function call stack
Every call pushes a frame. Every return pops one. That is the entire mechanism.
Before you start
When a function calls another function, both are technically "running" at the same time — the caller is paused, waiting for the callee to finish. The machine tracks all of this with something called the **call stack**.
Every function call pushes a new **frame** onto the stack, containing that call's local variables and where to return to. Every return pops the top frame off, freeing its space and jumping back.
This lesson watches frames stack up as one function calls another, and pop back down as returns fire. The name "stack" is not a metaphor — it is literally that shape in memory.
Stack
newest call at the bottomStep 1 of 8. main starts, r is not yet a number. `main` gets a frame containing its local `r`. The 4 bytes exist, but nothing has been written to them — `r` holds whatever was already there. The line `int r = triple(5);` cannot finish until `triple` produces a value, so the assignment is the last thing that will happen, not the first.
Step 01 / 08
main starts, r is not yet a number
main gets a frame containing its local r. The 4 bytes exist, but nothing has been written to them — r holds whatever was already there.
The line int r = triple(5); cannot finish until triple produces a value, so the assignment is the last thing that will happen, not the first.
#include <stdio.h>int add(int a, int b) { return a + b;}int triple(int n) { return add(n, add(n, n));}int main(void) { int r = triple(5); printf("%d\n", r); return 0;}