Tutelage

Lessons › coding and computer science · taught by Leo

Recursion explained: base cases and call stacks

Learn how recursion works, why a base case is essential, and how smaller calls return answers through the call stack.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

The same job repeated safely

Recursion is when a problem solves itself by asking for a smaller version of the same problem.

The trick is simple: keep shrinking the problem until it becomes tiny enough to stop.

That stop point is the base case. Without it, the calls would go forever.

Big idea: one rule, two parts

Every recursive solution needs two parts: a base case and a recursive case.

The base case says, do not call yourself again.

The recursive case says, solve a smaller version first.

Predict: what must recursion have?

Which part makes recursion stop?

  1. Base case
  2. Recursive case
  3. Loop counter
Show answer

Base case: Yes. The base case is the stopping rule.

Factorial by smaller pieces

Here is a worked example with factorial. Factorial means multiply by every whole number down to one.

We start with 5! and rewrite it as 5 times 4! because that is the recursive step.

Then we keep expanding until we hit the base case, which is 1.

Quick check: what comes next?

In the recursive formula for factorial, what should 4! become next?

  1. 4 × 3!
  2. 4 × 4!
  3. 3 × 2!
Show answer

4 × 3!: Correct. You shrink by one, so 4! becomes 4 times 3!.

Why the shrinking step works

Recursion works because each call makes the problem smaller, not different.

That gives you a path toward the base case instead of an endless repeat.

Call stack: nested return steps

On a computer, each recursive call is saved on a call stack like nested notes.

The newest, smallest call gets solved first, then the answers return back up.

So recursion has two directions: going down to the base case, and coming back up with answers.

Common mistake: no stopping rule

Watch out for the biggest mistake: forgetting the base case.

If nothing tells the function to stop, it keeps calling itself forever.

A good recursive solution always knows when to stop and what to return.

Your turn: choose the base case

Which rule is the base case for a function that counts down from n to 0?

  1. If n = 0, return 0
  2. If n > 0, call itself
  3. Always call itself
Show answer

If n = 0, return 0: Yes. When n is 0, you stop and return a value.

Recursion checklist to use solo

Use this checklist: first find the stop point.

Then make sure each call uses a smaller input.

Finally, make sure the answer comes back up from the smaller calls.

Recap: what recursion is

Recursion is a way to solve a problem by solving smaller versions of the same problem.

You need a base case, a shrinking step, and a return back up.

If you want, I can next show recursion with code in Python or with a real-life example like mirrors or folders.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app