How recursive function works in this example

Tell us what’s happening:
Hello there,

My question is: how the code returns a value of 120. I know that function is working until num = 0, but how multiplication action is storing the end value?

  **Your code so far**

function factorialize(num) {
if (num === 0) {
  return 1;
}
return num * factorialize(num - 1);
}

factorialize(5);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Factorialize a Number

Link to the challenge:

Hey! recursion makes use of the stack data structure to keep track of the function that is currently running as well as variables associated with that function. the functions on top of the stack are run first.

Recursive functions basically keep pushing functions on to the stack until a certain condition is met, in your case that condition is if num === 0. You can think of it as saying.

“I dont care what the factorial of n is, first find the factorial of num - 1 and keep repeating it until n === 0”

the first function’s execution starts, at this point the value of num is 5 which is greater than 0 so this code will run.

5 * factorialze( 5 -  1)

now the function

factorialze( 5 -  1)

is pushed to the stack which means this function’s execution will start and the outer function’s execution ( factorialize(5) ) will halt until it finds the result of factorialize(4).

in factorialize(4) the value of num is 4 which is greater than 0 then this code will run

4 * factorialze( 4 -  1)

and at this point factorialize( 3 ) will be pushed to the stack and the the execution can not go any further until it finds the result of factorialize(3).

and this will keep going until the condition ( num === 0 ) is met.
At which point, we return 1. this starts a backwards effect in which the value returned from one function is used to finish the execution of the function lower in the stack.

when the condition is met, we have something like this,

5 * fac( 4 ) * fac( 3 ) * fac (2) * fac( 1 ) *  fac( 0 )

here fac( 0 ) resolves to 1 so we can subsitute it here and this becomes,

5 * fac( 4 ) * fac( 3 ) * fac (2) * fac( 1 ) *  1

now if we have fac of 0, we can calculate fac(1) because it is just

1 * fac( 0 )
// which we can also write as 

1 * 1
// now we get this

5 * fac( 4 ) * fac( 3 ) * fac (2) * 1  *  1

// finding out the value of fac( 2 ) is just as easy
// because it requires us to substitute the value of fac(1)

5 * fac( 4 ) * fac( 3 ) * ( 2 *  1) * 1  *  1 

or

5 * fac( 4 ) * fac( 3 ) * 2 * 1  *  1

After returning a value, each function is pushed off of the stack and the value returned from it is used to calculate the function lower in the stack until the final result becomes

5 * 4 * 3 * 2 * 1 * 1

which is how you calculate the factorial of a number.

if you want to learn more about recursion in general, i would recommend you to check out this series

Hope this helped! :slightly_smiling_face:

1 Like

Hi @Macsagi82 !

You can also throw this into a debugger and slowly walk through it step by step so you can see what the computer is doing.

Hope that helps!

1 Like

I made a drawing a while ago explaining another recursion function:

1 Like

Thank you, after reading this line I got it.

It does help, thank you.

The fuction works until it doesn’t.

Good stuff, thanks a lot for this one !

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.