# Understand recursive functions

This post is free for all to read thanks to the investment Mindsers Blog's subscribers have made in our independent publication. If this work is meaningful to you, I invite you to become a subscriber today.

Recursive functions are one of my favorite development techniques. It's not a complicated thing. On the contrary, they are only functions used differently.

When we learn recursion, and more broadly, iteration in programming, we always stop at the classic iteration structures, loops. But they do not have a monopoly on iteration! In this article, I'll quickly tell you about another recursion structure.

## Recursion and loops

When we talk about recursion or iteration, we are usually talking about repeating things. In computing, it is the ability to repeat a defined quantity of instructions.

As we said, the classic way is to use loops for this. Programming languages generally offer several types of loops:

`while`

loops`do...while`

loops`for`

loops

But, there are still others. In JavaScript, for example, we can add these to the list:

`for...in`

loops`for...of`

loops

Generally, the loops added by the languages, in addition to the first three, are ultimately only specific variations of the first three loops.

I'm not going to explain loops to you here. They could be the subject of an article on their own. What must be remembered here is that loops are iterative structures offered by a language to allow a set of instructions to be repeated.

## Recursion and functions

Unlike loops, functions are not iterative structures as such. They make it possible to separate a sequence of instructions from the rest of the code in order to be able to execute them again later, on demand.

This last sentence looks a bit like an iterative structure, but here are the main differences that can be noted:

- A defined function may be called only once, or even never.
- If there is repetition (execution several times), it will be neither immediate nor linear unlike a loop.

A function is therefore not an iterative structure provided by the language. What will make a function recursive, and therefore allow iteration, is the use that the developer will make of it.

Let's see how to make a function recursive.

This function is not recursive. It adds `1`

to a number given as a parameter as many times as specified by the `times`

variable.

How to do now, to have the same result without the loops?

The recursive function does not change signature. It always takes the `base`

and `times`

variables as parameters. It always returns a number.

What changes here: **the function calls itself**. That's all.

Let's go through the steps again with the parameters `base=5`

and `times=3`

to understand:

- Is
`times`

less than or equal to`0`

? No, it is equal to`3`

, we continue. `result`

is equal to`base`

plus`1`

, i.e.`5 + 1`

, i.e.`6`

.- We pass the
`result`

and`times - 1`

to the`addOne`

function and we will directly return its return value when done. - Is
`times`

less than or equal to`0`

? No, it is equal to`2`

, we continue. `result`

is equal to`base`

plus`1`

, i.e.`6 + 1`

, i.e.`7`

.- We pass the
`result`

and`times - 1`

to the`addOne`

function and we will directly return its return value when done. - Is
`times`

less than or equal to`0`

? No, it is equal to`1`

, we continue. `result`

is equal to`base`

plus`1`

, i.e.`7 + 1`

, i.e.`8`

.- We pass the
`result`

and`times - 1`

to the`addOne`

function and we will directly return its return value when done. - Is
`times`

less than or equal to`0`

? Yes, we return`base`

, which is equal to`8`

.

By doing it step by step, we better understand how a recursive function can completely replace an iteration structure like the loop.

The secret to mastering recursive functions is practice. Below you will find some exercises that I have prepared for you to progress on the topic 👇

You can also get the Training Sheet: Recursive Functions as a one-time-purchase on the shop.

## This free post contains additional content for Full Mindsers Blog and Friend of Mindsers Blog members only

Join the 100+ developers and entrepreneur, premium members of our community and get full access to our library of private posts and additional content.

Become a memberAlready a member? Log in