0

Can anyone explain this to me? I'm having trouble wrapping my head around this concept of having function within a function.

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

  return num * factorialize(num-1);
}

factorialize(10);

This is not a loop right? Why does the function call itself continuously? How does it know when to stop? Wouldn't it just continue to factorialize negative numbers to infinity?

Appreciate the help and guidance as always.

  • It decreases `num` at every step and stops when `num` is 0 (since it just returns in that case). I'm sure there are already plenty of resources explaining recursion in detail. – Bernhard Barker May 29 '17 at 05:48
  • Perhaps this Q&A would help understand recursion in general and apply it to your function: https://stackoverflow.com/questions/25676961/understanding-how-recursive-functions-work – rishat May 29 '17 at 08:03
  • Thank you everyone! I am amazed at how helpful this community is! – Christopher Gunawan May 29 '17 at 13:17

5 Answers5

0

Recursion is a technique for iterating over an operation by having a function call itself repeatedly until it arrives at a result. You can read about it here

Dinesh undefined
  • 5,490
  • 2
  • 19
  • 40
  • This doesn't answer the question. Given the title, OP knows it's recursion, and given the post, OP knows the function is calling itself repeatedly. Also / alternatively, [answers containing little more than links elsewhere aren't “good answers”](https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers). – Bernhard Barker May 29 '17 at 06:01
  • @Dukeling got it. i will delete answer now – Dinesh undefined May 29 '17 at 06:05
0

Lets visualize:

factorialize(10)
  10*factorialize(9)
       9*factorialize(8)
          8*factorialize(7)
             7 *factorialize(6)
                 6 *factorialize(5)
                      5 *factorialize(4)
                          4 *factorialize(3)
                              3 *factorialize(2)
                                  2 *factorialize(1)
                                      1 *factorialize(0)
                                           1 (if num==0 return 1)
                                      1
                                  2
                              6
                          24
                     120  
                720
            5040
         40320
      362880
  3628800

Hope this helps!

Mithilesh Gupta
  • 2,800
  • 1
  • 17
  • 17
0

Each time the function "factorialize" runs your argument "num" get decremented by 1. So num will run from 10 to 1 and when 1 is returned it will reach its terminal case 0. It is only terminal because of your "if" clause ( your base case ) when num === 0, you return 1, and your recursive "factorizlize" is never reached, so it is never executed.

I am not really good with analogies. However, If it still doesn't make sense let me know.

Rick
  • 1,035
  • 10
  • 18
0

This is not a loop right?

Nope, it is not. It is a recursive function, a function that calls itself until a certain condition is met. In this case, is when num is equals to 0.

Why does the function call itself continuously?

Because the function is called in the return value of this function. It will continue calling the function itself, until num equals 0. In which case the function will exit and return 1.

How does it know when to stop?

The condition if (num === 0). The variable num gets subtracted if num isn't equal to 0 as stated in the code return num * factorialize(num-1);. Notice that the function returns fresh function call but with the parameter num - 1. When num becomes 0. The function returns 1.

Wouldn't it just continue to factorialize negative numbers to infinity?

Because we have this if (num === 0) condition. So the num parameter decreases each time the code return num * factorialize(num-1); gets called, and eventually the above condition gets fulfilled and the function exits.

We can break it down step by step:

  1. factorialize(10) gets called.
  2. num is not 10, so return num * factorialize(num - 1). In this case num is 10 and num - 1 is 9, so we are actually returning the following: 10 * factorialize(10 - 1).
  3. factorialize(9) gets called, then repeat step (1) and (2) until you get factorialize(0).
  4. When you reach factorialize(0), it will return 1. So the whole function effectively returns 10 * 9 * 8 * ... 1 * 1.

Makes sense?

Mμ.
  • 8,382
  • 3
  • 26
  • 36
0

Every iterative problem can be solved by using recursion. try to look at this code

var start = 1;
var end = 10;
var increment = 1;
document.write("loop output: ")
for(var num = start; num <= end; num = num + increment){
 document.write(num);
        document.write(" ");
}
document.write("\n function output:\n")

  function iteration(num) {

     //stop condition statement
     if (num > end) { 
        return 1; 
      }

      // inside code block
       document.write(num);
        document.write(" ");

      //incremention
       iteration(num+increment);
    }
iteration(start);

It uses internal functional frame stack for recursion purpose. for more about recursion look here . little practice is need to understand this concept.Hope make sense. Happy Coding !

GIRISH kuniyal
  • 740
  • 1
  • 5
  • 14