-2

please someone explain this to me

function func(x, n) {
  if (n == 1) {
    return x;
  } else {
    return x * func(x, n - 1);
  }
}
console.log(func(2, 4)); // 16

how do answer is 16

I could not understand more than this when if is false it going to else in the return section function call itself at the first input is the same value as x, ie 2 at the second input is n - 1 when function run again the value of x remains the same as 2 and the value of n becomes 3 that's mean 3 times if is false in the 4 times if if true and the execution loop ends and show answer in alert i can not figure out what number is multiplied by x per run or the answer should be the same as 2 because every time the function is executed, shouldn't the value of x be equal to 2?

Olian04
  • 6,480
  • 2
  • 27
  • 54
essencode
  • 3
  • 5
  • 4
    Check this https://stackoverflow.com/questions/717725/understanding-recursion, you have a recursive function. โ€“ arieljuod Oct 10 '20 at 22:25

3 Answers3

1

The function performs exponentiation. So func(x, n) is calculating xn.

It uses recursion, based on the following principle:

  • When n is 1, then xn = x1 = x
  • When n > 1, then xn = x.xn-1

For the second statement we can use the function with arguments x and n-1.

For the concrete example, func(2, 4), this is what happens:

x=2, n=4, so we get in the else block and evaluate:

2 * func(2, 3)

For this nested call of func we have x=2, n=3, and again we get in the else block for that:

2 * func(2, 2)

And again we have a nested call, which evaluates to:

2 * func(2, 1)

...and this final nested call gets into the if block and evaluates to:

2

Now we need to backtrack as each nested function call returns the result to its caller:

  • 2 is returned for func(2, 1) which turns 2 * func(2, 1) to 4
  • That 4 is returned for func(2, 2) which turns 2 * func(2, 2) to 8
  • That 8 is returned for func(2, 3) which turns 2 * func(2, 4) to 16
  • And that 16 is what you get in the output
trincot
  • 317,000
  • 35
  • 244
  • 286
1

If you are asking about how recursion works, then I will explain you with this basic flowchart diagram. enter image description here

I also edited your JS Code so it will provide you with clarification.

function func(x, n) {
  if (n == 1) {
    return x;
  } else {
    let function_returned = func(x, n - 1);
    console.log(x, "*", function_returned, "=", x*function_returned)
    return x * function_returned
  }
}
console.log(func(2, 4));
KayD
  • 746
  • 5
  • 15
  • 1
    Thank you all for answers, thank you KayD for your answer โ€“ essencode Oct 10 '20 at 22:44
  • In fact, the first time is always multiplied by 1 , I used plus and subtraction to see what happens in this case , in plus It gets like this 2+1=3 - 2+3=5 - 2+5=7 - 2+7=9 This means that the answer is wrong unless the first time the number is multiplied by 1 nnd next times plus 2*1=2 - 2+2=4 - 2+4=6 - 2+6=8 or 2*1=2 . 2-2=0 . 2-0=2 . 2-2=0 even for รท โ€“ essencode Oct 12 '20 at 11:26
0

It's a recursive power function. What it does is the following:

  1. 2^4 = ?
  2. 2 * 2^3 = ?
  3. 2 * 2 * 2^2 = ?
  4. 2 * 2 * 2 * (2^1) = ? // at this point we get to n == 1 and this is where result is just x which is 2 in your example. From this point we can start moving "back"
  5. 2 * 2 * (2 * 2) = ?
  6. 2 * (2 * 4) = ?
  7. (2 * 8) = ?
  8. 16 = ? // 16 is just 16, we're done. Your can read a bit about recursion to get a better idea on what is going on.