📄 Paper 1 · 4.1 Fundamentals of Programming
⭐ Pro
4.1.1g Stack Frames & Recursion
AQA 7517 · A-Level Computer Science · ~20 min read

The Call Stack

When a subroutine is called, the CPU stores execution context information on the call stack — a LIFO (Last In, First Out) data structure held in memory. Each entry on the call stack is called a stack frame.

What a Stack Frame Contains

Item storedPurpose
Return addressThe memory address to resume execution when the subroutine finishes
ParametersThe values passed as arguments to the subroutine
Local variablesVariables declared inside the subroutine
Saved registersCPU register values to be restored after the subroutine returns

Stack Frame Lifecycle

PUSH — When a subroutine is called, a new stack frame is pushed onto the top of the call stack.

POP — When the subroutine returns, its stack frame is popped off, restoring the previous frame and resuming from the stored return address.

Recursion

Recursion is a programming technique where a subroutine calls itself. Each call creates a new stack frame, building up the call stack until the base case is reached.

Requirements for a Valid Recursive Algorithm

  • Base case — a condition that stops the recursion (returns without making a recursive call)
  • Recursive case — the function calls itself with a smaller/simpler version of the problem, progressing toward the base case

Example: Factorial (n!)

FUNCTION factorial(n : INTEGER) RETURNS INTEGER
    IF n = 0 THEN
        RETURN 1          // Base case: 0! = 1
    ELSE
        RETURN n * factorial(n - 1)   // Recursive case
    ENDIF
ENDFUNCTION

Trace of factorial(3):

factorial(3) → 3 × factorial(2)
factorial(2) → 2 × factorial(1)
factorial(1) → 1 × factorial(0)
factorial(0) → 1              // base case

Unwinding:
factorial(1) → 1 × 1 = 1
factorial(2) → 2 × 1 = 2
factorial(3) → 3 × 2 = 6

Example: Fibonacci

FUNCTION fib(n : INTEGER) RETURNS INTEGER
    IF n ≤ 1 THEN
        RETURN n
    ELSE
        RETURN fib(n-1) + fib(n-2)
    ENDIF
ENDFUNCTION

Stack Overflow

Each recursive call adds a new stack frame. If the base case is never reached (or is reached too late), the call stack runs out of memory — this is called a stack overflow and causes a runtime error.

Causes of stack overflow from recursion:

  • Missing or incorrect base case
  • Recursive case that does not progress toward the base case
  • Extremely deep recursion (very large n)

Recursion vs. Iteration

FeatureRecursionIteration
Code eleganceOften more concise and elegant for problems with naturally recursive structure (trees, graphs)More verbose but explicit
Memory useBuilds up stack frames — higher memory overheadUses loop variable only — lower overhead
SpeedSlower due to repeated function call overheadGenerally faster
RiskStack overflow if base case not reachedInfinite loop if termination condition wrong
ReadabilityNatural for divide-and-conquer algorithmsNatural for counting and sequential tasks
Exam tip: AQA A-Level frequently asks you to: (1) trace a recursive function and show the call stack at each point; (2) state the base case and recursive case; (3) identify what would happen if the base case were removed. For stack frames, know the four items stored: return address, parameters, local variables, saved registers. The call stack = LIFO.
⚠️ Common Mistakes
  • Forgetting the base case — without it, recursion runs forever (stack overflow)
  • Thinking stack frames contain only variables — they also contain the return address and saved registers
  • Confusing stack overflow with heap overflow — stack overflow is specific to function call depth
  • Saying recursion is always better — it uses more memory and is slower; iteration is preferred when they're equivalent
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.1.1g Stack Frames & Recursion

8 questions · instantly marked · AQA 7517 standard

Q1State four items typically stored in a stack frame when a subroutine is called.[4]
✅ Mark scheme
Mark scheme
Return address [1]; parameters/arguments passed to the subroutine [1]; local variables [1]; saved CPU register values [1].
Q2What type of data structure is the call stack? State its key property.[2]
✅ Mark scheme
Mark scheme
A stack [1]; it is LIFO — Last In, First Out (the most recently pushed frame is the first to be popped) [1].
Q3Define recursion. State the two essential components of any recursive algorithm.[3]
✅ Mark scheme
Mark scheme
Recursion is where a subroutine calls itself [1]; base case — a condition that stops the recursion [1]; recursive case — calls itself with a smaller problem moving toward the base case [1].
Q4Trace factorial(4) step by step, showing the call stack building up and unwinding. What is the final result?[4]
✅ Mark scheme
Mark scheme
factorial(4) calls factorial(3); factorial(3) calls factorial(2); factorial(2) calls factorial(1); factorial(1) calls factorial(0) [1 for showing build-up]; factorial(0) returns 1 (base case) [1]; unwinding: 1×1=1, 2×1=2, 3×2=6, 4×6=24 [1]; final result = 24 [1].
Q5What is a stack overflow error in the context of recursion? Give two causes.[3]
✅ Mark scheme
Mark scheme
Stack overflow occurs when the call stack runs out of memory due to too many stack frames [1]; caused by: missing or incorrect base case [1]; recursive case that doesn't progress toward the base case [1]; or recursion depth too large for available stack memory [1] (any two for 2 marks).
Q6Give two advantages of iteration over recursion.[2]
✅ Mark scheme
Mark scheme
Any two: uses less memory (no stack frames built up) [1]; generally executes faster (no function call overhead) [1]; no risk of stack overflow [1]; easier to trace and debug [1].
Q7Give one advantage of recursion over iteration.[1]
✅ Mark scheme
Mark scheme
Recursion can produce more elegant/concise code for naturally recursive problems such as tree traversal, quick sort, or fibonacci [1].
Q8Identify the base case and recursive case in the following function: FUNCTION sumTo(n) → IF n=0 THEN RETURN 0 ELSE RETURN n + sumTo(n-1). What does sumTo(3) return?[3]
✅ Mark scheme
Mark scheme
Base case: n=0, RETURN 0 [1]; recursive case: RETURN n + sumTo(n-1) [1]; sumTo(3) = 3+2+1+0 = 6 [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 12
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Stack Frames & Recursion

10 questions · 10 minutes

← 4.1.1f Subroutines
7 of 70 · AQA 7517
4.1.2a Procedural Programming →