aımnova
DashboardMy LearningCrash CoursesStudy Plan

Aimnova site navigation

Stay in the loop

Get the latest study resources and updates

New features, study tips and exam insights — straight to your inbox.

IB Diploma

  • IB Past Papers
  • IB Study Notes
  • IB Question Bank
  • IB Mock Exams
  • IB Revision

IB Subjects

  • IB Math AA
  • IB Math AI
  • IB Economics
  • IB Business Management
  • IB Physics
  • IB Biology
  • View all IB subjects→

IB Past Papers

  • IB Math AA HL Past Papers
  • IB Math AA SL Past Papers
  • IB Math AI HL Past Papers
  • IB Math AI SL Past Papers
  • IB Economics HL Past Papers
  • IB Economics SL Past Papers
  • IB ESS Past Papers
  • View all past papers→

Study Resources

  • Study Notes
  • Question Bank
  • Mock Exams
  • Crash Courses
  • Flashcards
  • Revision Guide
  • Exam Skills
  • Command Terms
  • Grade Calculator
  • Exam Timetable 2026

Aimnova

  • Features
  • Pricing
  • For Teachers
  • For Schools
  • For Parents
  • About Us
  • Blog
  • Contact
aımnova

IB study, practice and feedback built around the real exam.

TermsPrivacyCookies·© 2026 Aimnova. All rights reserved.c1e448f

Aimnova is not affiliated with or endorsed by the International Baccalaureate Organization (IB).

NotesComputer Science HLTopic 6.4Recursion
Back to Computer Science HL Topics
6.4.45 min read

Recursion (Computer Science HL)

IB Computer Science · Unit 6

IB exam ready

Study like the top scorers do

Access a smart study planner, AI tutor, and exam vault — everything you need to hit your target grade.

Start Free

Contents

  1. 1A function that calls itself
  2. 2What the machine actually does
  3. 3When recursion earns its place
  4. 4Exam-style question
The big idea: A recursive function solves a problem by calling itself on a smaller version of the same problem, until it reaches a case small enough to answer outright.

Two parts, always

  • A base case that returns without recursing
  • A recursive case that moves towards it
def factorial(n):
    if n <= 1:          # base case — stops the recursion
        return 1
    return n * factorial(n - 1)   # recursive case — smaller n

# factorial(4) = 4 * factorial(3)
#              = 4 * 3 * factorial(2)
#              = 4 * 3 * 2 * factorial(1)
#              = 4 * 3 * 2 * 1 = 24
static int factorial(int n) {
    if (n <= 1) {            // base case — stops the recursion
        return 1;
    }
    return n * factorial(n - 1);   // recursive case — smaller n
}

// factorial(4) = 4 * factorial(3)
//              = 4 * 3 * factorial(2)
//              = 4 * 3 * 2 * factorial(1)
//              = 4 * 3 * 2 * 1 = 24
Both parts, or it never stops: No base case → it recurses forever until the call stack is exhausted: a stack overflow.

A recursive case that does not get closer → same result. factorial(n) calling factorial(n) never reaches 1.

Free preview

This is the free notes preview

You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:

  • FlashcardsLock in vocabulary and key terms with spaced repetition.
  • Practice questionsAnswer exam-style questions and get instant AI marking.
  • Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
  • Personalised study planA daily plan built around your exam date and weak areas.
Start Studying Free Full access to Aimnova Pro · cancel anytime
1

Each call gets a frame

A stack frame holds that call's own parameters, local variables and where to return to. factorial(4) puts four frames on the stack before any of them finishes.

2

Nothing returns until the base case

Every call is waiting on the one below it. The multiplications in factorial happen on the way back up, not on the way down.

3

Then they unwind

The base case returns 1, the call above multiplies and returns, and so on until the first call has its answer.

4

The stack is finite

Deep recursion uses real memory — one frame per call. A few thousand levels is often enough to exhaust it, which is why depth matters.

Why a stack, and not something else: Calls must return in exactly the reverse order they were made — the most recent first. That is last in, first out, so a stack is the only structure that fits.

This is the same call stack from B2.2, which is why the two topics connect.

Know your predicted grade

Take timed mock exams and get detailed feedback on every answer. See exactly where you're losing marks.

Try Mock Exams FreeYour first topic is free to keep • No credit card required
RecursionIteration
SuitsSelf-similar structures — trees, nested dataRepeating a fixed number of times
MemoryOne frame per levelConstant
RiskStack overflow if deepInfinite loop if the condition never changes
Reads asClose to the definition of the problemClose to the steps a machine takes
SpeedSlower — call overhead per levelUsually faster

Where recursion is the natural choice (the guide's examples)

UseWhy it is recursiveBase case
QuicksortPick a pivot, split the list into smaller and larger items, then quicksort each part the same wayA part with 0 or 1 items is already sorted
Traversing a binary treeVisit a node, then traverse its left subtree and its right subtree, each a smaller treeAn empty subtree
Fractal imagesA shape is drawn from smaller copies of itself, e.g. each branch of a fractal tree splits into two smaller branchesA branch shorter than a set length is not split
The honest answer on factorial: Factorial and Fibonacci are taught recursively because they are easy to show, not because recursion is the right choice for them — a loop is faster and uses constant memory.

Recursion genuinely wins where the data itself is nested: traversing a tree, walking a folder structure, parsing an expression. Saying that shows judgement.

How this is tested — you must identify both parts and explain the stack cost, not just describe self-calling. It comes up two ways:

Paper 2 — working with code

  • Explain the concept of recursion, 3-4 marks
  • Identify the base case in given code
  • State one application of recursion

Paper 2 — the algorithmic-thinking question

  • Compare recursion with iteration
  • Explain why deep recursion fails
The classic trap: Saying only "a function that calls itself". That is half the definition — without a base case it never terminates, and the answer must name both parts.
IB-style questionExplain[5 marks]

Explain what recursion is, using a worked example, and state one advantage and one disadvantage against iteration.

Model answer plan

See the answer broken into clear points, with guidance on how each point earns marks.

Claim your free topic

Try an IB Exam Question — Free AI Feedback

Test yourself on Recursion. Write your answer and get instant AI feedback — just like a real IB examiner.

A program searches a binary search tree for a name. one benefit of using recursion for this. [2 marks]

Related Computer Science HL Topics

Continue learning with these related topics from the same unit:

6.1.1Variables and data types6.1.2Substrings6.1.3Exception handling6.1.4Debugging
View all Computer Science HL topics
Improve your exam techniqueCommand terms, paper structure, and mark-scheme tips for Computer Science HL
Previous6.4.3Bubble and selection sortNextTracing recursion6.4.5

17 questions to test your understanding

Reading is just the start. Students who tested themselves scored 82% on average — try IB-style questions with AI feedback.

Start FreeView All Computer Science HL Topics