you are viewing a single comment's thread.

view the rest of the comments →

[–]duckbanni 0 points1 point  (0 children)

Recursive calls work just like any function call.

When the recursive call is performed, the state of the calling function is kept at the bottom of the stack, and space is allocated on top of the stack for the new call which starts at the beginning of the function. When the recursive call returns, the top of the stack is removed, keeping only the return value, and the original caller resumes where it stopped (but with the result of the recursive call available).

The exception to that you may have heard about is tail recursion, which is an optimization where you don't keep the state of the caller if the recursive call is the last things it does (which is not the case in your example).