← All lectures

Lecture 21 · 35 min

Recursion

Functions that call themselves — elegant solutions for problems naturally defined in terms of smaller subproblems.

Recursive functionsBase caseFactorialFibonacciInfinite recursion
Slide 1 / 14

What is recursion?

Recursion is when a function calls itself to solve a smaller version of the same problem.

Real-world analogy: Imagine you're looking for a document in a folder. You open the folder, but inside is another folder. You apply the same process to the inner folder. This continues until you find a folder with no subfolders (the base case).

Recursion is powerful for problems that are naturally self-referential: factorials, tree traversal, searching, sorting.

← / → navigateF fullscreenEsc exit⌘K search