IB Computer Science HL · Year 2 · Lesson 9

Recursion: Concepts, Tracing and Construction

Explain when recursion is useful, evaluate its limitations, and construct simple non-branching recursive functions in Python.

Paper 250 minutesB2.4.4 · B2.4.5
Today’s targets

What you need to be able to do

2 / 10
  • explain base cases, recursive cases and progress toward termination;
  • trace recursive calls and returned values;
  • explain advantages, limitations, complexity and memory implications of recursion;
  • connect recursion to suitable applications including fractals, binary trees and sorting;
  • construct simple non-branching recursive algorithms in Python.
B2.4.4B2.4.5
Paper 2 habitRead the command term and the data given. A technically correct answer still needs to answer the exact task.
Learn + retrieve

Rapid Recall Deck

3 / 10

Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.

B2.4.4 · Explain

Every recursive solution needs a stopping point

4 / 10
def sum_to(n):
    if n == 0:
        return 0
    return n + sum_to(n - 1)

Base case

n == 0 returns a result without another recursive call.

Recursive case

The function reduces n by 1 and calls sum_to() again.

Explain why this terminates for a positive integer n

Progress — n becomes smaller on each call.
Stop — eventually n reaches 0.
Return — the base result allows earlier calls to finish.

Each recursive call uses n - 1, so a positive integer moves closer to 0 on every call. When n == 0, the base case returns 0 without making another call. The pending calls can then return their results, so the recursion terminates.

Trace recursion

Follow calls down, then returns back up

5 / 10
TraceEvaluate sum_to(3) using the function from the previous page.

Write the call chain and the return chain

Calls down — show each argument value until the base case.
Returns up — substitute returned values to reach the final answer.
sum_to(3)
= 3 + sum_to(2)
= 3 + 2 + sum_to(1)
= 3 + 2 + 1 + sum_to(0)
= 3 + 2 + 1 + 0
= 6
B2.4.5 · Construct

Write simple non-branching recursion

6 / 10

Construct factorial(n)

Assume n is a non-negative integer. Return 1 when n == 0; otherwise return n × factorial(n - 1).

Base case — handle n == 0.
Recursive case — make exactly one recursive call with a smaller n.
Return the value; do not print intermediate results.
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)
Advantages + limitations

Recursion trades clarity for call overhead

7 / 10
AdvantagesRecursive code can closely match problems defined in terms of smaller similar subproblems. This can make some algorithms easier to express and reason about.
LimitationsEvery active call needs stack memory. Deep recursion can use substantial memory, add function-call overhead or exceed the recursion limit; a poor recursive design can also repeat work and produce high time complexity.

Explain one trade-off

An iterative loop and a recursive function both correctly calculate a countdown from n to 0. Explain why the recursive version may use more memory even if both perform a similar number of steps.

Recursive calls remain active until the base case is reached.
Call stack stores information needed to resume each pending call.

The recursive version creates a new function call for each smaller value of n. Those calls remain on the call stack until the base case is reached and the calls unwind, so additional stack memory is required. A simple iterative loop can usually update a small fixed set of variables without storing a chain of pending calls.

Applications

Use recursion where the problem repeats its own shape

8 / 10

Fractal image creation

A shape can contain smaller versions of the same shape, so the drawing rule can be applied recursively.

Binary-tree traversal

A tree contains subtrees with the same structural idea, making recursive traversal a natural conceptual fit.

Sorting algorithms

Algorithms such as quicksort can solve smaller partitions recursively.

Syllabus boundaryYou should understand these recursive applications, but B2.4.5 construction/tracing is limited to simple non-branching recursive algorithms in programming.

Explain suitability

Why is a recursively defined fractal a natural application of recursion?

A fractal is built from smaller regions that follow the same or a similar generation rule as the whole. A recursive function can apply the rule to a smaller scale on each call until a base condition such as minimum size or depth is reached.

Exam lens

Teacher-created Paper 2 practice

9 / 10

1. Using factorial(0) = 1 and factorial(n) = n × factorial(n-1) for n > 0, trace the return value of factorial(4).

2. Explain why a recursive function needs both a base case and progress toward that base case.

3. Construct countdown(n) so it prints n, recursively calls itself with n - 1, and stops after printing 0. Assume n is non-negative.

  1. 4 × 3 × 2 × 1 × 1 = 24.
  2. The base case stops further calls; progress ensures the arguments eventually reach that stopping condition. Missing either can lead to non-termination until the call stack limit is reached.
  3. def countdown(n):
        print(n)
        if n == 0:
            return
        countdown(n - 1)
Homework

Finish the learning cycle

10 / 10

IA — main task

Review any IA problem that has a naturally repeated or hierarchical structure. Only consider recursion if it genuinely improves the solution; do not add it simply to appear advanced.

Syllabus — short retrieval

Redo factorial(4) and the countdown construct without Reveal. Then explain aloud why tree traversal can be recursive even though today's construction practice is non-branching.