Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is recursion in Python

Understanding Recursion: A Beginner's Guide

Recursion can be a mind-bending concept when you're first starting out with programming. To put it simply, recursion is a technique where a function calls itself to solve a problem. Imagine a Russian nesting doll, where each doll opens up to reveal a smaller doll inside, and this repeats until you reach the smallest doll. Recursion works in a similar way, where a problem is solved by solving smaller instances of the same problem.

The Basics of Recursion in Python

In Python, a recursive function is one that calls itself in its definition. This might sound a bit like a paradox, but it's a powerful tool in a programmer's toolkit. To better understand this, let's look at a basic example:

def countdown(number):
    if number <= 0:
        countdown(number - 1)


In the countdown function above, we're printing a number and then calling the same function with the number decreased by one. The function continues to call itself with smaller numbers until it reaches zero, at which point it prints "Liftoff!" This is the essence of recursion: a problem (counting down from a number) is solved by solving a smaller instance of the same problem (counting down from the next smaller number).

The Importance of a Base Case

Every recursive function must have a base case, which is a condition that stops the recursion. Without a base case, the function would call itself indefinitely, leading to a stack overflow (which is essentially the computer version of an infinite loop). In the countdown example, the base case is if number <= 0:.

Recursion in Action: The Factorial Function

A classic example of recursion is the calculation of a factorial. The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2 * 1. Here's how you might write a recursive factorial function in Python:

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


In this code, factorial(5) will return 5 * factorial(4), factorial(4) will return 4 * factorial(3), and so on, until factorial(1) returns 1. The function then "unwinds", multiplying all these results together to give you 5!.

Visualizing Recursion with a Tree Structure

Recursion can also be envisioned as a tree structure. Each function call represents a node on the tree, and each branch represents a subsequent function call. In the factorial example, the top of the tree is factorial(5), which branches out to factorial(4), and so on, until the leaves of the tree, which represent the base case factorial(1).

Recursion vs. Iteration

You might be wondering why we use recursion when we could solve the same problems with loops (iteration). Sometimes recursion can provide a clearer, more elegant solution. For example, let's look at an iterative version of the factorial function:

def factorial_iterative(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result


While this code does the same thing, it may not be as immediately clear as the recursive version. However, it's important to note that recursion can be less efficient and more memory-intensive than iteration because it requires maintaining a stack of function calls.

Handling More Complex Recursion

Recursion can also be used to solve more complex problems, such as traversing directories, parsing complex data structures, or solving puzzles like the Tower of Hanoi. The key is to break down the problem into smaller subproblems that can be handled by recursive calls.

Recursive Functions Must Evolve

A common mistake when writing recursive functions is forgetting to ensure that each recursive call evolves toward the base case. If the state of the problem doesn't get progressively smaller or simpler, the recursion will never reach the base case, leading to infinite recursion and eventually a crash.

Debugging Recursive Functions

Debugging recursive functions can be tricky because of the multiple function calls. One useful technique is to add print statements to track the flow of the function calls and the changing state of the variables involved.

Conclusion: The Recursive Mindset

Recursion is a fundamental concept in computer science that can be applied to a wide range of problems. It requires a shift in mindset to think about problems in terms of base cases and smaller subproblems. As you practice writing recursive functions, you'll develop a deeper intuition for when and how to use recursion effectively.

Remember, recursion is like learning to ride a bike. At first, it might seem daunting and complex, but with practice and patience, it becomes second nature. So go ahead, try writing some recursive functions of your own, and watch as the elegant simplicity of recursion unfolds before you.