Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Recursion

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.

Structure

Every recursive function needs:

  1. Base case — a condition where the function returns directly without recursing
  2. Recursive case — the function calls itself with a smaller input

Example: Factorial

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.

Run

python recursion.py

Prompts for a non-negative integer and prints the factorial both ways.