Courses / Mathematics I
Programming Elements

Recursion

Mathematics I 185 words Free to read

Anatomy of Recursion

Recursion is a technique where a function calls itself on a smaller version of a problem until it reaches a directly solvable case. It parallels mathematical induction.

Every recursive function requires two core ingredients:

IngredientRoleExample (n!n!)
Base caseStops recursion directly0!=10! = 1
Recursive caseShrinks problem & calls selfn!=n×(n1)!n! = n \times (n-1)!

To compute 3!3!, the function descends to the base case, then unwinds upward: 3×2×1=63 \times 2 \times 1 = 6.

Rules and Pitfalls

Recursive calls must make strict progress toward the base case. If the subproblem does not shrink, the function recurses forever, triggering a stack overflow as pending calls pile up.

PropertyRequirement
TerminationMust reach base case
ConvergenceInputs must strictly decrease

Proving a recursive function correct is an induction. The base case establishes correctness, and the recursive step ensures larger instances build correctly on smaller ones.

Recursion

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

13practice questions
2interactive scenes

Programming Elements