recursion…recursion…recursion…

Ilknur Eren
1 min readSep 30, 2018

Put into practice, recursion is a code that calls upon itself. It can call upon itself until forever unless we put in a condition to stop once it reaches a certain point.

In code, a recursion example can be written as below:

function factorial(n){
if(n === 1){
return 1
}else{
return n * factorial(n-1)
}
}
factorial(5)

The example above is a classic example of a recursive code. The code above calls the integer n to be multiplied by one minus the current n. Once n reaches 1, the code returns 1 and the recursion reaches the final point and starts returning the value back to code.

The image above shows that factorial of 5 first returns 5 * factorial(4). The factorial of 4 returns 4 * factorial(3) and so on until n reaches 1. Factorial of 1 returns 1 in which the code goes back the code and fills in the factorial(n) with actual numbers.

One importing aspect to understand is that n is not one number. N is multiple numbers in recursion. N is 5, also 4, also 3, also 2, and finally 1.

https://www.youtube.com/watch?v=Mv9NEXX1VHc

--

--