DEV Community

Cover image for What is recursion in programming and when to use it?
Tomek Skupiński
Tomek Skupiński

Posted on • Edited on • Originally published at blog.thecode.xyz

What is recursion in programming and when to use it?

What is recursion in programming?

In computing, recursion is a method where the solution to a problem is found by breaking it down into smaller subproblems. This process is then repeated until the original problem is solved. The classic example of recursion is the Towers of Hanoi problem.

To better understand recursion, let’s look at an example. Let’s say you want to calculate the factorial of a number. The factorial of a number is the product of all the positive integers less than or equal to that number. For example, the factorial of 5 is 5! = 5 4 3 2 1 = 120.

You could calculate the factorial of a number using a loop like this:

def factorial(n):
    result = 1
    for i in range(1, n+1):
        result *= i
    return result
Enter fullscreen mode Exit fullscreen mode

However, this iterative solution is not very efficient. A more efficient solution is to use recursion.

 def factorial(n):
     if n == 1:
         return 1
     else:
         return n * factorial(n-1)
Enter fullscreen mode Exit fullscreen mode

This recursive solution is more elegant and efficient. When you call the factorial function with some value of n, it will:

Check if n is equal to 1. If it is, it will return 1. If n is not equal to 1, it will return n * factorial(n-1).

As you can see, this recursive solution breaks the problem down into smaller subproblems. It will keep breaking the problem down into smaller subproblems until it reaches a point where the subproblem is trivial to solve. Then, it will start solving the subproblems from the bottom up.

The factorial function is a simple example of recursion, but there are many other examples of recursion in computer programming. Other common examples include the Fibonacci sequence and quicksort.

When should you use recursion?

Recursion can be a very powerful tool, but it is important to know when to use it and when not to use it. Recursion is best used when the problem can be broken down into smaller subproblems that are similar to the original problem.

For example, the problem of sorting a list of numbers can be broken down into smaller subproblems: sorting a smaller list of numbers, sorting a list of numbers with the first element removed, etc. This is why the quicksort algorithm is recursive.

On the other hand, recursion is not always the best solution. If the problem cannot be broken down into smaller subproblems, or if the subproblems are not similar to the original problem, then recursion is probably not the best solution.

For example, the Towers of Hanoi problem can be solved recursively, but it would be much more difficult to solve iteratively.

When in doubt, it is usually best to try an iterative solution first. If an iterative solution is not possible or is too difficult, then you can try a recursive solution.

Pros and cons of recursion

Like any tool, recursion has its own set of pros and cons.

Some of the pros of recursion include:

Simplicity: Recursive solutions can often be simpler and easier to understand than iterative solutions.

Generality: Recursion can be used to solve problems that cannot be solved using iterative methods.

Flexibility: Some problems are easier to solve using recursion than iteration.

Some of the cons of recursion include:

Overhead: Each time a recursive function is called, there is some overhead involved. This can make recursive solutions slower than iterative solutions.

Memory usage: Recursive solutions often use more memory than iterative solutions because each time a recursive function is called, another stack frame is added to the call stack.

Debugging: Debugging recursive solutions can be difficult because it can be hard to trace the execution of the program.

When to avoid recursion

There are some situations where you should avoid using recursion.

Some situations where recursion might not be the best solution include:

  • Problems that can be solved using simple iteration
  • Problems that require only a small amount of data
  • Problems that are not well suited to being broken down into smaller subproblems

Summing up

Recursive solutions are not always the best solution, but they can be a powerful tool in your toolbox. If you are unsure whether or not to use recursion, try an iterative solution first. If an iterative solution is not possible or is too complex, then you can try a recursive solution.

Top comments (0)