Benas Lengvinas - 9 months ago 63

Javascript Question

I can't seem to fully understand the fundamentals off recursive functions. We have this code:

`function myself (n) {`

if (n <= 1) {

return 1;

}

return n * myself(n-1);

}

myself(5);

I get that 5 would be multiplied by 4 equals to 20 and then 20 multiplied by 3 and so on but what I don't quite get is how can 'n' be two different numbers in one function.

Answer Source

how can 'n' be two different numbers in one function

Variable scope exists for a function *invocation*, not a function definition.

Each call to `myself`

gets an `n`

variable of its very own, which is unrelated to the previously existing `n`

s.