Call stack walkthrough

How result = avg_numbers(6, 10) executes — step by step.
Click Next to advance. Watch three things: the program counter (red, bottom), which instruction is highlighted in code memory (left), and how the call stack (right) grows and shrinks.
Code memory
main()
0x1000int result;
0x1004call avg_numbers(6, 10)
0x1008result = ret_val
0x100C...
avg_numbers(int a, int b)
0x2000int c;
0x2004c = (a + b) / 2;
0x2008return c;
Call stack
↑ top (grows upward)
bottom
Program Counter 0x1004
Step 1 of 6

Stack entry colors

Caller's frame (main)
Parameters
Return address
Local variables

Vocabulary

Key terms from this walkthrough.

Program counter (PC)
A special register inside the processor that holds the memory address of the next instruction to execute. The CPU fetches from this address, then the PC advances. A call overwrites the PC with a function's address; a return overwrites it with the saved return address.
Stack frame
The block of memory on the call stack that belongs to one active function call. It bundles together everything that function needs: its parameters, return address, and local variables. Every call creates a new frame; every return destroys one.
Local variable
A variable declared inside a function. It lives in that function's stack frame and disappears when the function returns. In the example, c inside avg_numbers is a local variable.
Parameter variables
The values passed into a function by the caller. They are placed on the stack (or in registers) before the call, and the function reads them as if they were local variables. In avg_numbers(6, 10), the values 6 and 10 become parameters a and b.
Function arguments
The actual values the caller supplies at the call site. Arguments and parameters are two sides of the same coin: arguments are what goes in, parameters are what the function sees on the inside. In avg_numbers(6, 10), 6 and 10 are the arguments; inside the function they become the parameters a and b.
Return address
The memory address of the instruction to execute after the called function finishes. The call instruction saves this onto the stack before jumping. The return instruction pops it back into the PC so execution resumes in the caller.
Call stack
A region of memory that holds stack frames, one on top of another, in Last-In-First-Out order. When a function calls another, a new frame is pushed on top; when it returns, the top frame is popped off. The stack shrinks and grows as the program runs.
Memory address of a function
Every function is stored in the program's code memory at a specific address. This is where execution jumps on a call. In the example, main starts at 0x1000 and avg_numbers starts at 0x2000.
Memory address of variables in a stack frame
Each parameter and local variable sits at a specific address within its stack frame. The CPU usually accesses them relative to a base pointer (the start of the frame) — for example, parameter a might be at base + 4 and local c at base − 8. Different call = different addresses.