How I Taught Recursion in 45 Minutes
TeachingFeatured

How I Taught Recursion in 45 Minutes

8 min

How I Taught Recursion in 45 Minutes

Recursion is one of those topics that students either "get" immediately or struggle with for weeks. Here's how I broke it down.

The Stack of Books Metaphor

I started with a simple analogy:

"Imagine you're cleaning your room and you find a stack of books. You want to know how many books are in the stack. You can only see the top book. What do you do?"

Answer: You take the top book, count it (1), then look at the next one — and repeat.

java
public int countBooks(Stack<Book> books) {
    if (books.isEmpty()) return 0;           // base case
    books.pop();                              // remove top
    return 1 + countBooks(books);            // recursive case
}

The Whiteboard Flow

I drew three frames on the whiteboard:

  1. The callfactorial(5) with the stack frame
  2. The unwind — each frame returning to its caller
  3. The base case — highlighted in green, the "escape hatch"
java
public static int factorial(int n) {
    if (n <= 1) return 1;          // base case
    return n * factorial(n - 1);  // recursive case
}

Exercises I Gave

  1. Easy: Sum an array recursively
  2. Medium: Reverse a string recursively
  3. Hard: Tower of Hanoi

Key Takeaways

  • Always start with the base case — students need to see where the recursion stops
  • Draw the call stack — every single time
  • Use real-world metaphors — Russian dolls, mirrors facing each other, nested Matryoshka

Recursion is not magic. It's just a function that calls itself with a smaller problem. Trust the stack.