Recursive Function

Can anyone please explain me this?

1 Like

Ok, let me try :wink:
The problem you have, as far as I think, is “where is n updated?”, right?
what we do here is calling a function from within itself. Remember those images when you have a video-camera connectedto your tv and point it to your screen? The image is duplicated endlessly within each image like forming an endless tunnel. That’s just what we do here. First time we call the function we give it a number as ‘n’ argument. Say it’s 3.
From within that function we call the same function again (remember the tunnel) now handing ‘n-1’ so it’s 2. From within that function we again call the same function with n-1 (n was 2 now and so we call it with n=1). So we did actually update n for each new call ofthe function since the argument taken is not the old ‘n’ but the result of ‘n-1’ as given by the function call.
Does that make any sense?

Maybe that becomes a little easier when we change the function call and add a little line before:
after the

return 1;
let c=n-1
and call the function by using
return n * ftl©;
Now you see that each time you call the function from within itself it starts off with a new value for the ‘n’ argument.


Thank you very much Andreas! Your effort is highly appreciated! :grinning:


So the answer to why ‘n’ is updated is because thats how it works?

its just one way to do it. You could also use a loop, updating n each time and calculating until done.
probably even easier to do in that task but it might be much more typing in other tasks.

in php-language e.g. that would be:

$result=1; // start with 1 as initial result
for ($n=3;$n>1;$n–){ // Start with n=3, do as long as Variable ‘n’ > 1, minus 1 after each cycle)
$result=$result*$n; // multiply with current n-value
} // end of for-loop, jumps back to where it starts and checks if still $n>1
echo $result; // output result to screen

1 Like

Hey there,

n is being updated in the function call on the line return ftl(n - 1)

If n is 3, this line is the same as return ftl(3 - 1)

That means the function calls itself again, but with 2 as an argument.

The function will then call itself again, but with 1 as an argument, like this:
return ftl(2 - 1)

Then, when n is 1, this if statement stops the function from calling itself again:

if (n === 1) {
    return 1;

Does that clear things up? Let me know if you have any questions.

Hi, Lawrence here! So, the function ftl takes an argument n and if n is 1 then obviously return 1. Since n is 3, this happens.
console.log(3 times ftl(2)) = console.log(3 times 2 times ftl(1)) and since ftl1 is 1 it’s console.log(3 times 2 times 1). Another function that works the same is (times symbol is replaced by product() function):

function factorial(num) {
let numberFactorial;
if (num === 1) {
return 1;
return product(num, factorial(num - 1));

Love, Lawrence.

Many thanks, everyone! I perfectly understood Andreas’ explanation (alas!), But you’ve added a lot of interesting extras :slight_smile: :grinning:

Hy Ben,
Where can I get JavaScript exercises please. Algorithmic problems that will challenge me. The practice section is helpful, but I’ll appreciate it if I can get my hands on exercises outside Grasshopper.
Thank you .

I have a question please. I didn’t quite get how n = 3. Where is the 3 coming from?