Log in Sign up
Back to Discover
💻

Recursion (computer science)

technology Maturity 7-9

A computer can solve big jobs. It breaks them into small jobs. It does this by doing the same thing again.

recursiveTree.JPG
recursiveTree.JPG
This helps it finish the work. It is a smart way to think. Can you see patterns in nature?

41 words

A computer can solve big jobs. It breaks them into small jobs. It does this by doing the same thing again.

recursiveTree.JPG
recursiveTree.JPG

This is called recursion. It happens when a task calls itself. The task gets smaller each time. This helps the computer finish the work.

Sierpinski triangle turtle.gif
Sierpinski triangle turtle.gif

Every task needs a stop rule. This rule is the base case. It tells the computer when to stop. Without it, the computer never stops.

Some tasks use one rule. Other tasks use many rules. This can make the work harder.

Tower of Hanoi.jpeg
Tower of Hanoi.jpeg

Recursion helps us see patterns. You can see it in math. You can see it in art too.

111 words

Computers use a special way to solve problems. This way is called recursion. It happens when a function calls itself. A function is a set of instructions for a task.

recursiveTree.JPG
recursiveTree.JPG

Recursion works by breaking a big job into smaller parts. Each part is a smaller version of the same problem. To work well, recursion needs two main parts. The first is the recursive case. This part breaks the task down. It makes the problem smaller each time.

Sierpinski triangle turtle.gif
Sierpinski triangle turtle.gif

The second part is the base case. This is a stop rule. It tells the computer when to stop. The base case uses the simplest possible input. Without a base case, the computer might never stop. This can cause errors.

Tower of Hanoi.jpeg
Tower of Hanoi.jpeg

There are different kinds of recursion. Single recursion uses one self-call. Multiple recursion uses more than one. Some recursion is also indirect. This is when one function calls another. Then, that second function calls the first one back. This creates a chain of tasks. Recursion is a core idea in computer science. It helps us write code in a natural way.

185 words

Recursion is a very important idea in computer science. It is a way to solve a problem by using smaller versions of that same problem.

Recursive1.svg
Recursive1.svg
In programming, this happens when a function calls itself from within its own code. Most programming languages allow this to happen. Some languages, like Clojure, do not even have standard loops. They use recursion for everything instead. Even without loops, these languages are just as powerful as others.
Recursive2.svg
Recursive2.svg

A recursive function needs two special parts to work correctly. The first part is called the recursive case. This part breaks a big problem into smaller sub-problems. Each step must make the problem smaller so it gets closer to an end. The second part is the base case. This is a simple rule that provides an answer directly. The base case is very important because it acts as a stopping condition. Without a base case, the computer might try to run forever.

Sierpinski triangle turtle.gif
Sierpinski triangle turtle.gif

Math experts used recursion long before computers existed. People like Church, Gödel, Kleene, and Turing studied how it works. Their work helped make modern programming possible. In the late 1950s and early 1960s, it became a practical tool. John McCarthy created the LISP language in 1960 to use recursion. He showed it could work with symbols step by step. At the same time, the ALGOL 60 committee added it to their language.

recursiveTree.JPG
recursiveTree.JPG

There are many ways to use these rules in real life. One example is the Fibonacci sequence. This is a list of numbers where you add the two previous numbers together. The base cases for this are when the number is 0 or 1. Another example is the merge sort algorithm. This method sorts data by breaking it into smaller parts. You keep dividing the parts until they are only one element long. This is the base case for sorting.

Tower of Hanoi.jpeg
Tower of Hanoi.jpeg

Recursion can also describe how data is built. Some data, like a list of strings, can be defined by itself. You can say a list is either empty or it contains a string and another list. This allows a programmer to make lists of any size. There is also something called indirect recursion. This happens when one function calls a second function. Then, that second function calls the first one back. This creates a chain of tasks that work together.

396 words

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.svg
Recursive1.svg

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.svg
Recursive2.svg

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.JPG
recursiveTree.JPG

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.gif
Sierpinski triangle turtle.gif

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.jpeg
Tower of Hanoi.jpeg

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.

743 words
🖼️ 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
Up Next
💻
Theory of computation
Technology
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.