← Back to all tools

🔄 How Do Functions Call Themselves? — Recursion Explained

Watch recursive functions call themselves, build up a call stack, and unwind with results. Step through each call.

Designed with the WJEC specification in mind
CS A-Level Unit 3

🤔 What is recursion?

Recursion is when a function calls itself to solve a smaller version of the same problem. It's like a set of Russian dolls — each doll contains a smaller version until you reach the tiniest one!

🍕 Analogy: Imagine looking for your keys by asking "Are my keys here?" If not, ask the same question in the next room, then the next, until you find them (base case reached)!
⚙️

Choose a recursive function

Call Stack

Step-by-step

Select a function and click Visualise or Step.
📖

Key Concepts

Recursion = a function that calls itself. Every recursive function needs:

1. Base case — when to STOP (prevents infinite recursion)

2. Recursive case — calling itself with a SIMPLER problem

Call stack: Each call creates a new "frame" on the stack. When the base case is reached, frames unwind from top to bottom, each returning its result to the caller.

Warning: Too many recursive calls → Stack Overflow! Python has a default limit of ~1000 recursive calls.

📝

Exam Tips

  • A-Level: Always identify the base case and recursive case in any recursive function
  • Key point: Recursion uses more memory than iteration because of the call stack
  • Remember: Each recursive call creates a new stack frame — they unwind in reverse order
  • Common mistake: Forgetting the base case leads to infinite recursion and stack overflow