view raw
Benas Lengvinas Benas Lengvinas - 6 months ago 52
Javascript Question

understanding fundamentals of recursive functions

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);

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.


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 ns.