Python Fundamentals · Functions
factorial(4) calls factorial(3), which calls factorial(2)… Watch the frames stack up until the base case, then unwind with the answers.
def factorial(n):
if n <= 1: # the base case stops the recursion
return 1
return n * factorial(n - 1)
result = factorial(4)
print(result)factorial(). Its body does not run yet: it runs each time the function is called.if n <= 1: # the base case stops the recursion.n changes from 3 to 2.if n <= 1: # the base case stops the recursion.n changes from 2 to 1.