Understanding Recursion

Diego Jeanluck Linares Castillo
4 min readJul 4, 2022

Recursion is the way that how is specified a process based on its own definition.
But in programming terms is equal to using a recursive function. But…

What is a recursive function?

A function is recursive when it's called by itself inside of its own definition, either directly or indirectly (through other functions).

Some popular examples of problems recursive in nature:

  • Fibonacci
  • Factorial of a number
  • Go through graphs

Advantages and disadvantages of recursion:

Recursion like any other concept has advantages and disadvantages, we are going to see some of them know “why” and “when” to use recursion.

Advantages:

  • In most cases, recursion uses few lines of code, making the code “elegant” to the eye.
  • It's useful to explain and execute problems of recursive in nature (be efficient).
  • Usually is more simple to apply than that an iterative solution.
  • It’s useful to know the recursivity because we can use it to design algorithms.

Disadvantages:

  • High use of memory.
  • Isn’t efficient in most cases.
  • In some cases, it’s hard to understand (hardly to explain or that someone understands the code easily).

Example of a recursive function (_pow_recursion)

This function finds the result of x to the power of y with recursion.

This function allows the positive, negative, and floating numbers.

Parts of a recursive function

A recursive function has 3 important parts:

  • Base Case
  • Call itself (directly or indirectly)
  • Code

Base Case

Well the recursion is a kind of loop and like any other loop we have to break the loop and we do this with the Base Case.

A default case that always will be reached, because the other way we’ll get an infinite loop.

Call itself

To make able the recursion we must call the function inside itself like this.
We have two different calls to the function.

One is for when the exponent is positive and the other is when the exponent is negative.

Code

The “Code” in this case it’s all that our function does except the call itself and Base Case.

This function only exits the Base Case and two calls to the function.

Recursion on Stack

We’ll use the previous function (pow_recursion) to explain what’s happening in the stack.

We’ll use: x = 7, y = 5

When we call the function _pow_recursion, we’ll call a total of 5 times the function because we’ve to subtract 1 to the variable “y” by each iteration getting the result of a stack with 5 frames of the same function.

After that, we arrived at the Base Case (When “y” is equal to one), at this point, that function returns the number seven.

And we get to the previous frame where we continue the function since we left it. Multiplying 7 by “x” (x is 7 too). And we’ll do this until the function ends. Getting the result 16870.0!

When we end with the functions we’ve finished, we end the recursive function and we get our result. Getting an empty stack!

And that's all. I hope this information will be useful to you.

Thank you for reading.

--

--

Diego Jeanluck Linares Castillo

Autodidact | Passionate about technology | Holberton School Student