AQA 7517, section 4.1.1.15
When a subroutine is called, the computer has to remember three things. Where should the program carry on afterwards? What values were passed in? And which local variables does this call have? It stores all of this in a block of memory called a stack frame.
Stack frames are kept on a call stack, which is a stack data structure. If you haven't met stacks yet, see Stacks: the key idea is last in, first out.
What a stack frame holds
Each stack frame stores:
- the return address: the address of the instruction to go back to when the subroutine finishes;
- the parameters: the values passed into the subroutine;
- the local variables: those declared inside the subroutine.
How it works
- When a subroutine is called, a new stack frame is pushed onto the call stack.
- The subroutine runs, using the parameters and local variables in its frame.
- When it finishes, its frame is popped off the stack and the return address is used to carry on at the right place.
Because it's a stack, the most recently called subroutine is always finished first, which is what's needed when one subroutine calls another.
Worked example
def add_one(x):
y = x + 1
return y
def double_then_add(n):
d = n * 2
return add_one(d)
result = double_then_add(5)
print(result) # 11
Follow the call stack as the program runs. The top of the stack is at the top of each diagram.
1. Call double_then_add(5)
| double_then_add |
| n = 5 |
| return -> main |
2. It calls add_one(10)
| add_one |
| x = 10, y = ? |
| return -> in d_t_a |
| double_then_add |
| n = 5, d = 10 |
| return -> main |
3. add_one returns 11, its frame is popped
| double_then_add |
| n = 5, d = 10 |
| return -> main |
4. double_then_add returns 11, its frame is popped
(only the main program remains)
At each step, the frame on top belongs to the subroutine that is currently running. When add_one finishes, its frame is removed. The return address in its frame tells the computer to go back to the line in double_then_add that made the call, and the variables n and d are still there, safe in the frame below.
Why use a stack?
- It remembers the order of calls, so each subroutine returns to the right place.
- Each call gets its own copy of the parameters and local variables. This is why two calls to the same subroutine don't interfere with each other, which matters for recursion (4.1.1.16).
- Memory is automatically freed when a frame is popped.
Stack overflow
The call stack has a limited size. If too many frames are pushed without being popped, for example because a recursive subroutine never stops calling itself, the stack runs out of space. This is called a stack overflow. In Python it appears as a RecursionError.
Check yourself
Name the three items stored in a stack frame
The return address, the parameters and the local variables.
What happens to the stack when a subroutine is called, and when it ends?
When it is called, a new stack frame is pushed onto the call stack. When it ends, its frame is popped and execution continues from the return address.
Why is a stack the right structure for subroutine calls?
Subroutines finish in the reverse order that they were called: the most recent one finishes first. A stack is last in, first out, so the top frame is always the one currently running.
What is a stack overflow and what could cause one?
It happens when the call stack runs out of space. A common cause is a recursive subroutine that never reaches its base case, so frames keep being added.