The hard-to-understand part of this kind of recursion is that there are many different variables involved, all of which are called "num".
Every time you call a function, a new copy of all the local variables in that function is created ("allocated"). (The formal name for the area in which this happens is "stack frame".)
The definition of local variables includes parameters, and the mechanism works even when the calling function and the called function are the same function definition. Combined, these two mechanisms results in a cascade of variables with different values existing at the same time (but only one in the currently active invocation).
A good tool for understanding this is a debugger with a stack frame viewer which allows you to expand the local variables in all currently existing frames, not just the topmost one. This allows you to check that yes, there is in fact a num
with value 1 and a different num
with value 2 at the same time.
num
is not a global variable that gets updated at each call. Rather,num
is local to each function call / activation. In other words, there is one copy ofnum
for each active call torecursiveFunction
. Each copy is defined locally in one function activation and does not influence the value of other copies, living in other activation records ofrecursiveFunction
.