2

I already checked forum, I saw a few with a similar title but not one that answered my question. I noticed if I create a recursion function the return statement returns after the termination statement it looks like. Could someone explain how this works to me? thanks

function recur(n=10){
  if(n===0){
    return "";
  }
  console.log(n);
  return "A" + recur(n-1);
}

recur()

The end result is:

10
9
8
7
6
5
4
3
2
1
"AAAAAAAAAA"

I expected it to return A for each instance of the function, because I thought every statement in the function definition would be called for every instance of the function, like this:

10
"A"
9
"A"
8
"A"
7
"A"
6
"A"
5
"A"
4
"A"
3
"A"
2
"A"
1
"A"

So to reiterate why didn't the function return A like I was expecting, what pattern of how a function operates am I misunderstanding?

5 Answers 5

4

Working it out like this might help your understanding:

recur(5) = "A" + recur(4)
         = "A" + ("A" + recur(3))
         = "A" + ("A" + ("A" + recur(2)))
         = "A" + ("A" + ("A" + ("A" + recur(1))))
         = "A" + ("A" + ("A" + ("A" + ("A" + recur(0)))))
         = "A" + ("A" + ("A" + ("A" + ("A" + ""))))
         = "A" + ("A" + ("A" + ("A" + ("A"))))
         = "A" + ("A" + ("A" + ("AA")))
         = "A" + ("A" + ("AAA"))
         = "A" + ("AAAA")
         = "AAAAA"

So recur(5) returns "AAAAA". You should be able to extend this reasoning to show that recur(10) returns "AAAAAAAAAA".

You are printing the value of n, an integer, each time, and not printing the result of each recursive invocation of recur. The "AAAAAAAAAA" you are seeing at the end is the result of your console (shell, REPL, ...) displaying the result of everything you execute; in this case you see this at the end of your invocation of recur(), which is the same as recur(10).

If you want to “trace” the function, you can assign the result to a variable and then return it. Try this:

$ node
> function recur(n=10){
...   if(n===0){
.....     return "";
.....   }
...   let result = "A" + recur(n-1);
...   console.log(n, result);
...   return result;
... }
undefined
> recur()
1 'A'
2 'AA'
3 'AAA'
4 'AAAA'
5 'AAAAA'
6 'AAAAAA'
7 'AAAAAAA'
8 'AAAAAAAA'
9 'AAAAAAAAA'
10 'AAAAAAAAAA'
Sign up to request clarification or add additional context in comments.

Comments

2

I assume you're executing this in a browser console, node shell, or something similar. The code outputs each value of the argument (except when it's 0) and doesn't output any "A" at all. The shell/console simply echoes the value returned by the function; in this case your top-level call to recur(). The reason the top-level call evaluates to "AAAAAAAAAA" is that each function call appends an "A" to the value of the recursive call. (The base case of n===0 returns an empty string.)

If you want to print "A" each time, you'll have to add your own output statement to the code. The shell will still print the return value for the top-level call after it returns.

Comments

2

You are returning 'A' from the function, and NOT printing it everytime. New A is concatenated to the return value on each recursive call. For the result you want you should do:

function recur(n=10){
  if(n===0){
    return "";
  }
  console.log(n);
  console.log("A");
  return recur(n-1);
}

Comments

1

Try running this, it may make it more visually apparent what is happening.

function recur(n=10){
  if(n === 0){
    return "";
 }
 var recursiveResult = "A" + recur(n-1);
 console.log(n);
 console.log(recursiveResult);

 return recursiveResult;
}

console.log(recur(10));

A recursive function should not return the same result for each "loop" of the recursive function. Otherwise, you don't need recursion :).

enter image description here

Comments

0

Avoid using "instance of a function" to mean "invocation of a function". Technically they are quite different. There is one instance of recur in the code example, which is invoked (i.e. called) eleven times.

None of the first ten invocations can return until the expression they are returning, ("A" + recur(n-1)'). has been evaluated. The eleventh invocation returns first. The previous invocations return in reverse order of their call, after and because the expression they are returning has finished being evaluated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.