How does recursion allow a program to solve a problem by calling itself?

Created from Britannica articles using AI. AI can make mistakes, so verify using Britannica articles.

In computer programming, recursion is a technique where a procedure, function, or algorithm calls itself one or more times to solve a problem.1

Rather than solving a complex task in a single step, recursion breaks the problem down into smaller, more manageable instances of the same problem. The process functions as follows:

  • Self-Reference: The function executes its logic by invoking itself, effectively creating a chain of nested operations.1
  • Base Case: To prevent infinite execution, the process must include a specified condition—often called a base case—that stops the self-calling sequence.1
  • Resolution: Once this condition is met, the program completes the pending operations in reverse order, returning values from the last call back to the first, ultimately resolving the original problem.1

This approach is conceptually similar to mathematical induction, where a property is established by relating it to known, simpler values. By repeatedly applying a routine operation, the program can define or compute complex results without needing to reference infinite sets of data.2