Visualized
Recursion Explained Visually: How fact(4) Builds and Unwinds the Call Stack
· Recursion explained with the call stack | andar se kaise chalta hai
Watch recursion run on the call stack: fact(4) calls fact(3), fact(2) and fact(1), adding a frame each time. The base case returns 1 and the answers unwind to 24. Forget the base case and Python raises RecursionError.
The code
def fact(n):
if n == 1:
return 1
return n * fact(n - 1)


![Animated sliding window over the array [2, 1, 5, 1, 3, 2]: a three-number window adds the new number and subtracts the old one, showing sums 8, 7, 9 and 6 and a maximum of 9.](https://i.ytimg.com/vi/NM5paB6_7wg/maxresdefault.jpg)