Recursion Inquiry

Hello,

Just a quick question:

Why, in this recursion, would the final return of ‘1’ (after ‘num’ equates to ‘0’) not actually return the number 1?

Regards :slight_smile:

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

If num is 0 then this function will return 1 as it will execute the 2nd else only.

If num is 1 then the function will execute the last else and return

1 * factoralize(0)

That is it will return 1 (as 1 * 1 is 1)

If the num is 2 then the function will return

2* factoralize(1)

So it will return 2

Etc etc

1 Like

Ah, yes!

So rather than simply returning ‘1’ it is returning ‘num’ * 1.

Awesome. I totally get it now :+1: