Nepedia
Back to Discover
Advanced Reader
💻

Recursion (computer science)

technology Maturity 7-9 Next article →

Recursion is a fundamental method in computer science for solving complex problems. It works by breaking a problem down into smaller instances of the exact same problem. In programming, this is achieved through functions that call themselves from within their own code. This approach is so powerful that some functional programming languages, such as Clojure, do not even include built-in looping constructs. Instead, these languages rely entirely on recursion to perform tasks. Despite lacking traditional loops, these recursion-only languages are Turing complete. This means they are just as capable of solving problems as imperative languages that use standard control structures like loops.

Recursive1
Recursive1

A properly designed recursive function must contain two distinct parts: the base case and the recursive case. The base case is a specific condition that provides a direct result without further recursion. It typically handles the simplest or smallest possible inputs. This part is essential because it acts as a stopping condition. Without a base case, a function might enter an infinite regress. This can lead to non-termination or a stack overflow error, where the computer runs out of memory. For example, when calculating a factorial, the rule that 0! equals 1 serves as the base case.

The recursive case describes how to transform the original problem into smaller sub-problems. Each step must move the input closer to the base case to ensure the process eventually ends. This structure is very similar to mathematical induction. In an inductive proof, you prove a base case and then an inductive step to show a theorem holds for all inputs. In recursion, the recursive case assumes the function works for a smaller version of the problem. It then uses that result to solve the current, larger problem. If the reduction step fails to approach the base case, the algorithm may get trapped in an infinite loop.

Recursive2
Recursive2

Recursion has a rich history rooted in mathematical logic. Early researchers like Church, Gödel, Kleene, and Turing developed the theories of recursive functions and computability. These mathematical foundations eventually made programming recursion possible. It became a practical tool for programmers in the late 1950s and early 1960s. John McCarthy was a key figure in this era. He created the LISP programming language in 1960. McCarthy demonstrated that recursion could be a core feature for processing symbols step by step. Around the same time, the ALGOL 60 design committee introduced recursion as a major feature. This was a significant shift because, previously, programmers could only use loops.

recursiveTree
recursiveTree

There are different types of recursion used depending on the task. Single recursion occurs when a function contains only one self-reference. This is often used for simple tasks like a linear search or calculating a factorial. Multiple recursion involves two or more self-references within a single function. A common example is a depth-first search used to traverse trees. While single recursion is generally more efficient, multiple recursion is often more fundamentally recursive. Another type is indirect, or mutual, recursion. This happens when function A calls function B, and then function B calls function A. This creates a chain of functions that call each other to solve a problem.

Sierpinski triangle turtle
Sierpinski triangle turtle

Recursion is also used to define complex data structures through self-referential definitions. Inductive definitions specify how to construct data. For instance, a list of strings can be defined as either an empty list or a structure containing a string and another list. This allows programmers to create lists of any finite size. Coinductive definitions are different because they specify how to perform operations on data. These are often used for data structures that are potentially infinite, such as infinite streams. Corecursion is a related technique used in lazy programming languages. It allows a program to compute specific, finite portions of an infinitely large or precise result.

Tower of Hanoi
Tower of Hanoi

Many famous algorithms rely on these recursive principles to function. The merge sort algorithm uses recursion to divide and sort subarrays until they reach the base case of a single element. The Fibonacci sequence is defined by adding the two previous numbers, with the base cases being 0 and 1. Binary search uses recursion to repeatedly divide a search interval in half. Tree traversals use recursive cases to process subtrees until they reach leaf nodes. Even complex mathematical grammars, like those used to describe arithmetic expressions, use recursion. This allows for the creation of incredibly complicated expressions like (5 * ((3 * 6) + 8)) using simple, repeating rules.

739 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (5)
File:recursiveTree.JPG
recursiveTree.JPG
File:Sierpinski triangle turtle.gif
Sierpinski triangle turtle.gif
File:Recursive1.svg
Recursive1.svg
File:Recursive2.svg
Recursive2.svg
File:Tower of Hanoi.jpeg
Tower of Hanoi.jpeg
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.