Skip to content

Recursion

Recursion is a programming technique where a function calls itself to solve a smaller version of its problem until it reaches a base case which is a condition that stops the recursion. A classic example is calculating a factorial, where n! = n * (n-1)!.

Factorial using Recursion
def factorial(n):
if n == 0 or n == 1:
return 1 # Base Case
else:
return n * factorial(n - 1) # Recursive Call
print(factorial(5)) # Output: 120