0

I'm new and have been working to try and rationalize this in my brain but can't seem to understand it. First the way that many will recognize using a simple "for" loop:

function power(base, exponent){
 var result = 1;
  for(var i = 0; i < exponent; i++){
   if(exponent == 0)
    return 1;
   else
    result *= base;
 };
 return result;
}

In this section I am reading about recursion and talking about how a function can call itself as long as it doesn't cause a stack overflow. The code is as follows:

function power(base, exponent) {
  if (exponent == 0)
return 1;
  else
return base * power(base, exponent - 1);
}

console.log(power(2, 3));

What I'm having an issue is understanding how it actually works, this what I think is happening:

After it moves on past the first "if" statement it moves to the "else" and calls itself to return to the top of the if statement, minus 1, each time till it reaches 0 when it will just return the "result". Is that right? Or am I missing something entirely?

Ctfrancia
  • 1,248
  • 1
  • 15
  • 39
  • you asking or explain recursion what you do and need ...? – Bhargav Chudasama Oct 10 '17 at 10:59
  • That's almost right, it will return the result to the caller function which will return it's result and so on up the stack till you get the final result. – Daniel Oct 10 '17 at 11:01
  • Bhargav, I was asking if I was right with my assumption, thanks linas mnew I'll look at that as well (didn't see it earlier) – Ctfrancia Oct 10 '17 at 11:08

2 Answers2

2

ok, lets look at in steps:

you call pow(2, 3)

first, it runs this code:

function power(base, exponent) {
  if (exponent == 0)
    return 1;
  else
    return base * power(base, exponent - 1);
}

substituting in the values:

function power(base /* 2 */, exponent /* 3 */) {
  if (3 == 0)
    return 1;
  else
    return 2 * power(2, 2);
}

to hasten this answer, i will present it in a table from now on:

First call resolves to

1) 2 * power(2, 2);

2) 2 * (2 * power(2, 1))

3) 2 * (2 * (2))

(on this last one, exponent is one, so it returns base or 2)

each time it loops, it times the answer by the base essentially

just like the loop, in the example,

2 * (2 * (2)) === 8, so power(2, 3) === 8

notrota
  • 1,048
  • 10
  • 21
0

Recursion isn't iterative, it doesn't make the function go back anywhere, it makes the function launch another instance of itself to further solve its result.

Let's consider an even simpler example. This function counts down from given number to 0 and yells "FIVE" and the current number every time it's divisible by 5:

function foo(anumber)
{
    if(anumber <= 0)
    {
        console.log("Reached 0, done.");
    }
    else
    {
        if(anumber % 5 == 0)
        {
            console.log("FIVE - " + anumber);
        }
        foo(anumber - 1);
    }
}

foo checks if the number is equal or lower than zero, and if not, checks if its divisible by 5, if yes it yells, and regardless it launches another foo to check a number lower by one. When an instance of foo finally reaches 0 the chain doesn't continue and the stack collapses.

This simple use of recursion is used pretty much only in functional programming, which dictates that both variable changing and iterative loops are bad for the code and cause a mess. Generally it's simpler and usually much more RAM-efficient to use a loop.

Recursion reaches its potential only with more complicated examples, but I'm sure with the basics explained understanding those won't be a problem.

Deuxis
  • 227
  • 1
  • 11
  • Yes, this helps as well. First time coming across recursion and so it can be difficult for my mere mortal brain to comprehend this at first. I must just practice this more and play with it till it finally clicks. Thanks for the time and effort, I appreciate it! – Ctfrancia Oct 10 '17 at 12:37