When you call a function recursively, they add the new function to a stack until have a final result (return
). When it occurs, you go back return by return in the stack.
An Execution example:
First Round wacky(4, 6)
:
-
4 <= 1
? No, so callwacky(4 - 1, 6 - 1)
Second Round
wacky(3, 5)
:-
3 <= 1
? No, so callwacky(3 - 1, 5 - 1)
Third Round
wacky(2, 4)
:-
2 <= 1
? No, so callwacky(2 - 1, 4 - 1)
Fourth Round
wacky(1, 3)
:1 <= 1
? Yes- return 3;
return 3 + 4
;
-
return 3 + 4 + 5
;
-
return 3 + 4 + 5 + 6
;
solved What is returned by the call wacky 4,6? also how to calculate this recursion?