The variable n
does not have to be updated. We're making a new call to factorial
with a reduced value of n
. Each call to factorial
only works with one n
value. Consider how this works.
Say we call factorial(3)
, so n
is 3. 3 is clearly not less than or equal to 1, so we get down to the return statement. We're going to multiply something by 3, but we don't know what yet. We then call factorial(2)
.
That starts a NEW call to factorial
, unrelated to the original. In this call, n
is still greater than 1, so we get to the return
statement, and we start multiplying 2 by something. That something calls factorial(1)
.
In THIS call to factorial, n
is less than or equal to 1, so we return 1.
Now we go back out to the second call, where we were in the middle of a multiplication. That line was basically:
return 2 * factorial(1)
which we now know is
return 2 * 1
so the second invocation returns 2. We now back out to the original invocation, where we had
return 3 * factorial(2)
factorial(2) returned 2, so this becomes
return 3 * 2
which returns 6 to the original caller.
n
never gets updated. It's just that each new call to the function gets a reduced value.