Recursive function for calculating
n! implemented in Python:
Behind the scenes, each recursive call adds a stack frame (containing its execution context) to the call stack until we reach the base case. Then, the stack begins to unwind as each call returns its results:
A demonstration should make things clearer. Let’s calculate
1 + 2 + 3 ⋅⋅⋅⋅ + 10 using recursion. The state that we have to maintain is (current number we are adding, accumulated sum till now) .
❤️ Brainstash Inc.