Recursion vs. Iteration in GCSE Computer Science
In GCSE Computer Science (AQA, OCR, Edexcel), you are expected to master standard iterative algorithms—loops like for and while. But what happens when a function calls itself?
Welcome to Recursion: an advanced algorithmic technique that elegant code relies on.
1. What is Recursion?
A recursive function is simply a function that calls itself from within its own code block. While an iterative loop repeats an action using a counter or condition, recursion breaks a problem down into smaller sub-problems until it reaches the simplest possible version.
Core Anatomy of a Recursive Function:
- Base Case: The stopping condition. Without a base case, the function calls itself indefinitely, causing a Stack Overflow error.
- Recursive Case: The block of code where the function calls itself, moving closer to the base case with modified arguments.
2. Case Study: Calculating Factorials (n!)
The factorial of a positive integer n (written as n!) is the product of all positive integers less than or equal to n.
E.g.
5! = 5 x 4 x 3 x 2 x 1 = 120.
6! = 6 x 5 x 4 x 3 x 2 x 1 = 720.
0! = 1
1! = 1
Approach A: The Iterative Solution (Standard Loop)
This is the standard approach taught in most GCSE programming lessons:
def factorial_iterative(n):
result = 1
# Loop from 1 up to n
for i in range(1, n + 1):
result *= i
return result
print(factorial_iterative(5)) # Output: 120
Approach B: The Recursive Solution (Advanced)
Notice how mathematical definition n! = n x (n-1)! directly maps into recursive Python code:
def factorial_recursive(n):
# 1. BASE CASE: Stop when n reaches 1 or 0
if n <= 1:
return 1
# 2. RECURSIVE CASE: Function calls itself with (n - 1)
else:
return n * factorial_recursive(n - 1)
print(factorial_recursive(5)) # Output: 120
3. How the Call Stack Works Under the Hood
When factorial_recursive(5) runs, Python pushes each function call onto a Call Stack in memory until the base case is satisfied:
factorial_recursive(5) ──> 5 * factorial_recursive(4)
factorial_recursive(4) ──> 4 * factorial_recursive(3)
factorial_recursive(3) ──> 3 * factorial_recursive(2)
factorial_recursive(2) ──> 2 * factorial_recursive(1)
factorial_recursive(1) ──> RETURNS 1 (BASE CASE REACHED)
Unwinding the stack back up:
Return 1 --> 2 * 1 = 2 --> 3 * 2 = 6 --> 4 * 6 = 24 --> 5 * 24 = 120!
4. Iteration vs Recursion Comparison
| Feature | Iteration (Loops) | Recursion |
|---|---|---|
| Definition | Repeats a block using for / while |
Function invokes itself repeatedly |
| Termination | Loop condition becomes False |
Base case is triggered |
| Memory Usage | Low (constant memory allocation) | Higher (stack frames stored per call) |
| Code Elegance | Can require complex loop variables | Clean, short, & mathematical |
๐ก GCSE Exam & Paper 2 Revision Tip
In your GCSE Paper 2 exam, pseudocode questions almost exclusively test iteration. However, top-tier Grade 9 questions occasionally test your ability to trace recursive functions. Always search for the Base Case first when tracing recursive pseudocode line-by-line!
5. Practice Challenge for Students
Try copying the recursive code and heading over to our interactive Python Console on the Computer Science page to test your solution! Build a Recursive Countdown Function that counts down from n to 0 and prints "Blast off!".
