Introduction

Let’s understand recursion step by step, with an example you can trace on paper. A function that calls itself sounds like a bug, not a technique β€” when my teacher first wrote one on the board, I was sure she’d made a typo. But recursion is one of the most elegant ideas in programming, and once you see it work, you’ll spot it everywhere: in folder structures, family trees, even the way a fern leaf repeats its own shape.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

What Is Recursion?

Recursion is when a function solves a problem by calling itself on a smaller piece of the same problem. Instead of looping, the function keeps handing itself tinier versions of the job until the piece is so small the answer is obvious.

The main idea

Picture two mirrors facing each other β€” each mirror holds a smaller copy of the other, down and down until the reflections get too tiny to see. Recursion works exactly like that. Every recursive function has two parts: the recursive case (the mirror β€” "do it again, but smaller") and the base case (the wall behind the last mirror β€” "stop here, the answer is obvious").

In simple words

  1. Check: is this the base case? If yes, return the simple answer β€” no more calls.
  2. If not, shrink the problem a little and call yourself with the smaller version.
  3. Combine the smaller answer with the current step and return it upward.

Recursion is also where algorithm efficiency starts to matter β€” after this, my Big-O notation guide will make a lot more sense.

Let's Solve an Example: factorial(4)

The factorial of 4 means 4 Γ— 3 Γ— 2 Γ— 1. Here's the recursive version β€” read it, then watch the calls pile up:

def factorial(n):
    if n == 1:          # base case
        return 1
    return n * factorial(n - 1)   # recursive case

Call 1 β€” factorial(4): 4 isn't 1, so it calls factorial(3) and waits. Python keeps this waiting call on a pile called the call stack.

Call 2 β€” factorial(3): Still not 1 β€” calls factorial(2) and waits. The pile grows.

Call 3 β€” factorial(2): Calls factorial(1) and waits. Three calls are now stacked up, holding their breath.

Call 4 β€” factorial(1): Base case! No more calls β€” it simply returns 1.

The unwind: factorial(2) wakes up and computes 2 Γ— 1 = 2. factorial(3) computes 3 Γ— 2 = 6. factorial(4) computes 4 Γ— 6 = 24. Answer: 24. Most beginners get stuck imagining all of this at once β€” don't. Just follow one call at a time, like I did here.

Here's the whole stack as a picture, plus a short animation walking through it:

Step-by-step visual

Animated step-by-step visual

The Two Rules (Break One, Regret It)

Every recursive function lives or dies by two rules. Rule 1: always have a base case β€” a condition where the function stops calling itself. Rule 2: every recursive call must move closer to that base case (ours shrinks n by 1 each time).

Honestly? Forgetting the base case is the #1 beginner mistake. Without it, the function calls itself forever until Python gives up and throws a RecursionError. I've done it, you'll do it β€” and now you'll know exactly what that error means. The call stack itself is a classic data structure: last in, first out.

Recursion vs Loops: Which One?

In my opinion, learn loops first β€” they're simpler to trace. But recursion shines for problems that are naturally nested, like walking through folders inside folders. Here's the honest comparison:

RecursionLoop
Best forNested / tree-like problemsSimple repetition
ReadabilityElegant for the right problemEasier for most beginners
RiskStack overflow if carelessInfinite loop if careless
MemoryUses the call stackNearly none

Test Your Understanding

Question: What happens if a recursive function has no base case?

  1. It returns 0 and stops
  2. It calls itself forever until Python stops it with an error
  3. It automatically switches to a loop
  4. Nothing β€” it works fine

Answer: B β€” without a base case there's nothing to stop the self-calls, so Python eventually raises a RecursionError.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

Related Articles

Conclusion

Recursion is a function calling itself on smaller pieces until it hits a base case β€” mirrors within mirrors, then the answers bubbling back up the call stack. Your homework: write a countdown(n) function that prints n, n-1, … down to 1 using recursion. Trace it on paper like we did with factorial(4). The moment the unwinding clicks in your head, you'll never unsee it.

Never miss a new article

Subscribe to The Geeks Guru newsletter and get every new article in your inbox.

Loading