Introduction to recursion
Introduction to recursion
Introduction to Recursion
Recursion is the behavior of a function where it calls itself. Such functions are called recursive. Unlike a loop, recursive calls do not just repeat — they work "inside" each other, each waiting for the deeper call to finish before continuing.
Every recursive function must have two parts:
- A base case — a condition where the function returns a result directly without calling itself. Without this, the function calls itself forever and crashes.
- A recursive case — where the function calls itself on a smaller or simpler version of the problem.
Factorial
The factorial of is defined as , with the base case .