Code Companion
Java

Programming technique · B2.4.4, B2.4.5 · Higher Level only

Recursion

Recursion occurs when a function or method calls itself on a smaller instance of the same problem. A reachable base case stops new calls; the pending calls then return in reverse order.

IB DP CS HL standards B2.4.4 and B2.4.5: Explain recursion, its applications, advantages and limitations, then construct and trace simple non-branching recursive algorithms in a programming language.

The three things every recursive design needs

Base case

A case that finishes without making another recursive call.

Recursive case

The case that performs the current step and makes exactly one smaller self-call in the simple non-branching model.

Progress

The recursive argument must move toward a reachable base case. Otherwise calls continue until the runtime fails.

A simple non-branching example

The precondition for this model is seconds >= 0. Every non-base call reduces seconds by one, so the base case at zero is eventually reached. The example deliberately uses a countdown rather than the summation problem in the challenge.

Trace both phases

PhaseCallWhat happens
WindingshowCountdown(3)prints 3, then calls showCountdown(2)
WindingshowCountdown(2)prints 2, then calls showCountdown(1)
WindingshowCountdown(1)prints 1, then calls showCountdown(0)
BaseshowCountdown(0)prints Launch and returns without another call
Unwindingreturn to showCountdown(1)that call has no more work and returns
Unwindingreturn to showCountdown(2)that call returns
Unwindingreturn to showCountdown(3)the original call completes
Runtime call stack: each active call has its own parameter value and return point. The runtime manages these call frames; you are not manually building the stack data structure from T11.

Check your understanding

Answer each question before opening the model answer.

  1. What makes a base case different from a recursive case?

    Reveal model answer

    A base case finishes without making another recursive call. The recursive case makes the self-call.

  2. Why is seconds - 1 important in the example?

    Reveal model answer

    It guarantees measurable progress toward the reachable base case seconds == 0 for the approved non-negative inputs.

  3. What happens after showCountdown(0) returns?

    Reveal model answer

    The pending calls unwind in reverse order. In this example they have no further statements, so each simply returns to its caller.

  4. Does recursion automatically use less memory than iteration?

    Reveal model answer

    No. Recursive calls require active call frames, so the simple depth-n model uses O(n) auxiliary call-stack space while a comparable loop may use O(1) auxiliary space.

Compare with an iterative solution

QuestionRecursive modelIterative model
Time for this simple countdownO(n)O(n)
Auxiliary spaceO(n) call depthO(1) fixed variables
Possible advantageCan match a recursively defined or self-similar problem directlyOften simpler for straightforward repetition
Possible limitationCall overhead, depth-dependent memory and harder tracesMay be less direct for naturally recursive structures

Where recursion can be useful

Conceptual applications include fractal image generation, traversing binary trees and recursive sorting algorithms. The syllabus construction ceiling here is deliberately narrower: simple non-branching recursive algorithms only. You are not required to construct branching tree recursion or recursive quicksort on this page.

Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Recursive Sum

Challenge ID: PC-T16-C01 · Standards: B2.4.4, B2.4.5

Construct a non-branching recursive sumTo method for non-negative integers. The base case returns 0 and the recursive case must make exactly one call on a smaller value. Hand-trace sumTo(4) through both the call phase and return phase, then test 0, 1 and several positive inputs.

Scaffold available

Recursive Power

Challenge ID: PC-T16-C02 · Standards: B2.4.4, B2.4.5

Construct a recursive power(base, exponent) method for non-negative integer exponents. Use exponent == 0 as the base case and one recursive call on exponent - 1. Trace power(3, 4), explain how the argument moves toward the base case, and compare the recursive version with an iterative loop in terms of clarity and auxiliary memory.

Scaffold available

Recursion Diagnosis

Challenge ID: PC-T16-C03 · Standards: B2.4.4, B2.4.5

Create three small non-branching recursive methods: one correct, one with a missing or unreachable base case, and one whose argument moves away from the base case. Predict what each will do before running it, then correct the two defective versions. Finish with a short explanation of one situation where recursion models the problem naturally and one limitation involving call overhead or depth-dependent memory use.