Skip to content
Codescope

The function call stack

Every call pushes a frame. Every return pops one. That is the entire mechanism.

core8 min8 steps

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 bottom
main()
rint
?
0x7ffd6a20
?

Step 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.

01 / 08

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.

function-call-stack.cC
#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;}