I am not able to understand this basic recursion example.When I executed this code. The output was 43210.Should not the output be 44444, as the value of num is updated to 4 to the latest recursive call or how is the value of num getting changed?Please explain, any help is appreciated.
Recursion explanation
recursion
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.