Nepedia
Back to Discover
Fluent Reader
💻

Recursion (computer science)

technology Maturity 7-9 Next article →

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
Recursive1

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
Recursive2

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

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
recursiveTree

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

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.

388 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.