A function that calls itself to solve a problem by reducing it to a smaller instance of the same problem. See Recursion (computer science) — Wikipedia.
Every recursive function needs:
- Base case — a condition where the function returns directly without recursing
- Recursive case — the function calls itself with a smaller input
n! = n × (n-1)! (recursive case)
0! = 1 (base case)
Both iterative and recursive versions are shown. The recursive version directly mirrors the mathematical definition.
python recursion.pyPrompts for a non-negative integer and prints the factorial both ways.