Free sample handout · IB Computer Science · B2.4.4–B2.4.5 · HL

Recursion — Student Handout

The printable companion to the recursion lesson: definitions to learn, annotated code, a blank trace table to complete, and practice questions with worked answers at the end. Every lesson in the library has a handout built like this one.

Print this page (Ctrl/Cmd + P) to hand it out on paper.

Section 1 · Key terms to learn
Recursion

A problem-solving technique where a function calls itself to solve a smaller instance of the same problem.

Base case

The condition under which the function returns a value directly, without calling itself again. This is what stops the recursion.

Recursive case

The part of the function that calls itself with a smaller or simpler input, moving closer to the base case each time.

Call stack

The memory structure holding one frame per function call that has started but not yet finished.

Stack frame

One call's block of memory on the stack, storing its local variables and the line to return to.

RecursionError

The Python exception raised when recursion depth exceeds the interpreter's limit — almost always a missing or unreachable base case.

Section 2 · Annotated code
def factorial(n):
    if n == 0:              # base case
        return 1
    # recursive case:
    return n * factorial(n - 1)

print(factorial(5))     # → 120
Line 2–3 — the base case. 0! is defined as 1, so this returns immediately with no further call.
Line 5 — the recursive case. n! is written as n × (n−1)!, so the function calls itself with an argument one smaller.
Why it terminates — each call reduces n by exactly 1, so n must eventually reach 0 and hit the base case.
Order matters — the base case is checked before the recursive call is made. Reverse them and the function never stops.
Section 3 · Complete the trace

Trace factorial(4) by hand. Fill in what each call returns. Remember: nothing is multiplied until the base case has returned.

CallnCalls nextReturns
factorial(4)4factorial(3) 
factorial(3)3  
factorial(2)2  
factorial(1)1  
factorial(0)0— (base case) 

How many stack frames exist at the deepest point?  

Section 4 · Practice questions
Question 1 [2 marks]

State the two parts every recursive function must have, and state the purpose of each.

Question 2 [3 marks]

A student writes a recursive function to reverse a string but omits the base case. Describe what happens when the function is called, and outline the fix.

Question 3 [4 marks]

A programmer replaces a working loop that sums 100 000 numbers with a recursive function. Explain two disadvantages of the recursive version.

Question 4 [4 marks]

Construct a recursive Python function count_down(n) that prints every integer from n down to 1, then prints "Done".

Answer key · worked model answers
Q1. The base case, which returns a value directly and stops the recursion (1); and the recursive case, which calls the function again with a smaller input so it moves toward the base case (1).
Q2. With no base case the function keeps calling itself on an ever-shorter string (1), but nothing stops it once the string is empty, so calls continue until Python's maximum recursion depth is exceeded and a RecursionError is raised (1). The fix is to add a base case returning the string itself when its length is 0 or 1, before the recursive call (1).
Q3. Each call adds a stack frame storing its own local variables and return address (1), so memory grows in proportion to the number of elements rather than staying constant as in a loop (1). With 100 000 elements the depth exceeds Python's recursion limit (1), so the program raises RecursionError and produces no result, while the loop completes (1).
Q4. One correct solution — base case identified (1), correct recursive call with a decreasing argument (1), print before the call so output descends (1), "Done" printed once at the base case (1).
def count_down(n):
    if n == 0:              # base case
        print("Done")
        return
    print(n)
    count_down(n - 1)     # recursive case
Every lesson comes with a handout like this

Close to 100 lessons across the 2027 syllabus, each with a printable handout, a unit revision booklet, and exam practice with worked answers.

See the matching deck Get full access