Skip to content
Visualized

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

fact
def fact(n):
    if n == 1:
        return 1
    return n * fact(n - 1)
Interview SheetPractise the patterns behind interview problemsProblems grouped by pattern, each with the idea first and the code in four languages.