Imagine you’re standing in front of a mirror, holding another mirror. What do you see? An endless tunnel of reflections.

That’s kind of what recursion is in programming: it’s a function that calls itself as part of its own process. It keeps repeating—”reflecting”—until it reaches a stopping point, something we call the base case.

In simple terms, recursion is when a function solves a problem by solving a smaller version of the same problem, and continues to do this until it reaches a case simple enough to solve directly.


🧮 Let’s Talk About Factorials

If you’ve ever taken a math class, you’ve probably heard of the factorial of a number. It’s written like this: n! and pronounced as “n factorial.”

So what’s 4!?

It’s:

4! = 4 × 3 × 2 × 1 = 24

But let’s look at it a different way:

4! = 4 × 3!
3! = 3 × 2!
2! = 2 × 1!
1! = 1  ← This is our base case.

See that chain? Each factorial builds on a smaller one. That’s recursion.


🧑‍💻 Writing It in Python

Let’s write a simple recursive function to calculate the factorial of a number:

def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n - 1)

Let’s walk through factorial(4):

  • factorial(4) returns 4 * factorial(3)
  • factorial(3) returns 3 * factorial(2)
  • factorial(2) returns 2 * factorial(1)
  • factorial(1) returns 1 (this is our base case!)

Then Python climbs back up the chain:

  • factorial(2) becomes 2 * 1 = 2
  • factorial(3) becomes 3 * 2 = 6
  • factorial(4) becomes 4 * 6 = 24

Magic! ✨


❗ Why a Base Case Is Important

Without a base case, recursion would go on forever… or at least until Python throws an error like:

RecursionError: maximum recursion depth exceeded

Always define a stopping point for your recursive function.


🧠 What Makes Recursion So Powerful?

  • You can solve problems that naturally break down into smaller pieces—like tree structures, directories, or mathematical formulas.
  • It often results in cleaner, shorter code.
  • It’s a key concept in functional programming.

But here’s the catch: not all problems need recursion, and too much recursion can make your code slow or hard to understand.


✅ Summary

Recursion is like telling a story within a story. A function calls itself to solve a smaller piece of a problem, continuing until it hits the base case—the simplest version of the problem. It’s elegant, poetic even, but must be used wisely.


📝 Practice & Review Questions

  1. Explain in your own words what recursion is.

  2. Identify the base case in the following function:

    def sum(n):
        if n == 0:
            return 0
        return n + sum(n - 1)
    
  3. Predict the output of factorial(3) using the recursive function:

    def factorial(n):
        if n == 1:
            return 1
        return n * factorial(n - 1)
    
  4. What happens if the base case is missing in a recursive function?

  5. Challenge: Write a recursive function that counts down from a number to 1:

    countdown(3)
    # Output:
    # 3
    # 2
    # 1
    

<
Previous Post
🧳 Python Nested Functions: A Function Within a Function
>
Next Post
📝 JavaScript contenteditable and execCommand(): How to Build a Mini Text Editor.