Recursion ftl from fundementals 2

From the screen shot below, i’d like to know does the computer screen print 6 or does it print 1? Because by the time 3 goes down to 1 in the recursion of the ftl function, it is supposed to end at returning.

1 Like

1st call: n=3, the function will not return anything until the value of ‘3 * ftl(2)’ is known. calculation of ftl(3) is STILL RUNNING.

2nd call: n=2, the function will not return anything until the value of ‘2 * ftl(1)’ is known. calculation of ftl(2) is STILL RUNNING.

3rd call: n=1, the function does return 1 to…

… the 2nd call, so that the calculation of ‘2 * ftl(1)’ can be DONE. Now the 2nd call returns 2 to…

… the 1st call, so that the calculation of ‘3 * ftl(2)’ can be DONE. Now the 1st call returns 6…

…which is the output.

Hey, you lost me here:

"3rd call: n=1, the function does return 1 to…

… the 2nd call, so that the calculation of ‘2 * ftl(1)’ can be DONE. Now the 2nd call returns 2 to…

… the 1st call, so that the calculation of ‘3 * ftl(2)’ can be DONE. Now the 1st call returns 6…

…which is the output."

How does the computer making the 3rd call that returns to 1, to the 2nd call again then the first call just to print 6?? That doesnt make sense… shouldnt it just print the string 1?

dont worry, everybody needs some time to understand recursion.
remember nested function calls like in the following similar but simplified programm:

/* ============================== /
function f3() {

return someValue
};
/
------------------------------------------------------- /
function f2() {

return someExpression * function3
};
/
------------------------------------------------------- /
function f1 {

return someExpression * function2
};
/
------------------------------------------------------- /
console.log(f1);
/
============================== */

console.log is calling f1 and WAITING for the return value
f1 is calling f2 and WAITING for its return value and
f2 is calling f3 and WAITING for its return value.
f3 is passing a return value back to f2 and afterwards is terminating
f2 is passing a return value back to f1 and finally is terminating
f1 is passing a return value back to console.log and finally is terminating as well

can you see, that function f1 WILL NOT TERMINATE but will keep running as long as its return value is not known, WAITING for the return value of f2? same there - function f2 WILL NOT TERMINATE as long as its own return value is not known.

so when f3 is called, both, f1 and f2 are STILL in the PC memory, STILL RUNNING:
f1 is waiting for the return value of f2 and
f2 is waiting for the return value of f3.

f3 terminates by returning a value to the calling function (which is f2),
and only now f2 can calculate its own return value and only now
f2 terminates by returning a value to the calling function (which is f1).
Now f1 can calculate its own return value and submit it to the calling function log().

Hi there! ftl(3); means
you’re passing an argument of 3 to ftl(),
ftl checks that it is not 1,
ftl calls ftl(3 -1) or ftl(2),
ftl(2) calls ftl(1),
and here’s where the real fun begins…
ftl(1) returns 1,
ftl(2) returns 2 times 1, or 2,
ftl(3) returns 3 times 2, or 6 so 6 gets logged.

I dont understand why everything under “where the fun begins” happens. Isnt the console supposed to stop running the code once the return 1 function is reached and then print the whole number 1 on the screen?

return values are ALWAYS passed back to the calling function.

in this example 1 is NOT being return TO console.log() but to ftl(2).
in the same way 2 is NOT being returned TO console.log() but to ftl (3).
and in the same way 6 is being returned to the calling function/method which is console.log()

Please get out of your brain, that there is only one ftl working in the memory. EVERY TIME ftl is called - no matter it is called by console.log() or by ftl itself - ADDITIONAL (!) memory space is RESERVED FOR THAT SPECIFIC FUNCTION CALL and it will be released only when THAT SPECIFIC FUNCTION CALL is proceeded to the end BY RETURNING A VALUE to the calling function

you can help yourself by having your snippet do some additional outputs for you:
my suggestion:

var no = 0;
var call = ["(not needed)", "1st", "2nd", "3rd", "..."];

function ftl(n) {
  no++;
  console.log("ftl(" + n + ") here. I'm the " + call[no] + " COPY of ftl in the memory.");
  if ( n === 1 ) {
    console.log("ftl(" + n + ") here. I'm gonna return 1 and terminate. BYE");
    return 1;
  }
  console.log("ftl(" + n + ") here. Gonna call ftl("+(n-1)+") now and will be waiting for an answer.");
  var returnValue = n * ftl( n - 1 );
  console.log("ftl(" + n + ") here again. Got an answer of ftl("+(n-1)+"), gonna return " + returnValue + " and terminate. BYE");
  return returnValue;
}

/*
The output of the console will be as follows: 

    console.log(ftl(3)) here. Gonna call ftl(3) now and write what ftl(3) will return to me in the last line
    ftl(3) here. I’m the 1st COPY of ftl in the memory.
    ftl(3) here. Gonna call ftl(2) now and will be waiting for an answer.
    ftl(2) here. I’m the 2nd COPY of ftl in the memory.
    ftl(2) here. Gonna call ftl(1) now and will be waiting for an answer.
    ftl(1) here. I’m the 3rd COPY of ftl in the memory.
    ftl(1) here. I’m gonna return 1 and terminate. BYE
    ftl(2) here again. Got an answer of ftl(1), gonna return 2 and terminate. BYE
    ftl(3) here again. Got an answer of ftl(2), gonna return 6 and terminate. BYE
    6
*/

Because when ftl(3) and ftl(2) are called, they are STILL RUNNING. Then 1 is returned, ftl(2) is still running so IT returns 2. ftl(3) is STILL RUNNING so it returns 6 and 6 get logged to console.