Juan Carlos Angulo
Recursion algorithms
CS Fundamentals

Recursion algorithms

JU
Juan Carlos Angulo

Software Engineer & Technical SEO Consultant

· 3 min read

Recursion Algorithms: Mastering the Art of Problem Solving

By: Juan Carlos Angulo, Senior Tech SEO & Software Engineer

Recursion trips up a lot of people the first time they see it: a function calling itself to solve a smaller version of the same problem. Once it clicks, it's hard to unsee. This article covers how recursion actually works, where it's useful, and where an iterative solution is just the better call, with code examples in Python and C++.

Table of Contents

What is Recursion?

Recursion is the process in which a function calls itself directly or indirectly to solve a problem. A recursive algorithm typically consists of two main parts: a base case and a recursive case. The base case defines the condition under which the function will stop calling itself, while the recursive case breaks down the problem into smaller subproblems.

Here's a simple illustration of a recursive function:

unknown node

In this example, countdown calls itself with a decrementing value of n until it reaches zero.

The Mechanics of Recursion

When a recursive function is executed, the following sequence occurs:

  1. Function Call Stack: Every time a function is called, it’s added to the call stack. This stack keeps track of function calls and their respective parameters.
  2. Base Case Evaluation: The function checks whether the current state meets the base case condition. If it does, it returns a result.
  3. Function Execution: If the base case isn't met, the function executes its logic and calls itself with modified parameters.
  4. Returning Values: Once the base case is reached, values are returned down the call stack back to the original function call.

Visualizing the Call Stack

Consider the example of calculating the factorial of a number recursively, say factorial(3):

  1. factorial(3)3 * factorial(2)
  2. factorial(2)2 * factorial(1)
  3. factorial(1)1 * factorial(0)
  4. factorial(0) → returns 1 (base case)

Resulting in the following calculations when unwinding:

  • factorial(1) returns 1
  • factorial(2) returns 2 * 1 = 2
  • factorial(3) returns 3 * 2 = 6

This stack of function calls is critical to understanding how recursive algorithms operate.

Types of Recursion

Recursion can be categorized into two primary types: direct recursion and indirect recursion.

Direct Recursion

In direct recursion, the function calls itself within its own body. It is the most common form of recursion and is seen in the countdown example discussed earlier.

Here is another simple example:

unknown node

In this case, the power function directly calls itself to evaluate the power of a number.

Indirect Recursion

Indirect recursion occurs when a function calls another function, which in turn calls the first function again. This can make the flow of control somewhat more complex.

Here’s an example:

unknown node

In this case, function_a calls function_b, and function_b calls function_a, demonstrating indirect recursion.

Benefits and Drawbacks of Recursion

Benefits

  1. Simplicity: Recursive solutions are often more concise and easier to understand compared to their iterative counterparts, particularly for complex problems.
  2. **Problem Decomposition

See Also

JU
Juan Carlos Angulo

Software Engineer & Technical SEO Consultant

I'm Juan Carlos Angulo, a Software Engineer and Technical SEO Consultant based in Lima, Peru, with over four years of professional experience. My work sits at the intersection of software development and search engine optimization: technical SEO audits (crawlability, Core Web Vitals, Schema.org, indexation) combined with full-stack development in Next.js and Payload CMS. I help businesses grow their organic visibility by fixing issues directly in the code, no intermediaries involved. I also run juan-tech.com, a bilingual technical blog for developers and tech professionals across Latin America and Spain.

Related Posts