Recursion is the process a procedure goes through when one of the steps of the procedure involves invoking the procedure itself (Wikipedia)
$n!$ and $(n-1)!$ are related by a simple formula
This is a recursive definition of the factorial function
The base case – This condition(s) that ends the recursion
The recursive call – This is when the function calls itself
def factorial (n: int): # Base case, the recursion ends here if n <= 1: return 1 # Recursive call, the function calls itself return factorial(n-1) * n
Important — Make sure the base case is reachable, otherwise the function will run forever
Each time a function is called, a new frame is added to the call stack
The frame contains the function’s arguments and local variables
When the function returns, the frame is removed from the stack
Recursion is the process a procedure goes through when one of the steps of the procedure involves invoking the procedure itself
Base case(s):
Recursive call :