Recursion: Functions That Call Themselves Until a Base Case: the Gist's illustration
Programming & Software · Coding Fundamentals

Recursion: Functions That Call Themselves Until a Base Case

Explains recursion as a function calling itself, with a base case that stops the calls. It also covers where recursion fits, plus its memory cost and lack of speed advantage.

4-minute listen 6 sources 2-question quiz Fact-checked
Listen to this Gist
0:00 3:40

Read along

The full Gist, word for word. The small numbers link to the sources for each fact.

What would happen if a set of instructions told you, partway through, to go back and follow those same instructions again? At first it sounds like a trap with no way out. Computer scientists call this pattern recursion, and it comes with its own way to stop. At its heart, recursion describes a problem by pointing back to that very same problem.1

In programming, the idea takes the form of a function that calls itself.5 That call can happen directly, or it can happen indirectly.5 A function written in this style has a name of its own: it is known as a recursive function.2 The concept was not invented for computers.1

It was carried over straight from mathematics, a field that contains many expressions defined in terms of themselves.1 One way to picture recursion is a set of nesting boxes. Opening one box reveals another box of the same shape, only smaller, and opening that one reveals yet another. The picture raises an obvious puzzle, though.

If every box holds another box, where does the opening ever end? In recursion, that ending point has a special name: the base case.1 The base case holds the answer to the problem in its very simplest form.1 In the boxes picture, it would be the smallest box, the one with nothing left inside.

The base case marks the moment when a recursive function no longer calls itself.4 Its purpose is to keep the function from looping endlessly.4 To see why that matters, consider what happens without one. A function that has been called may call itself once more, and then again, and then again.5

With no base case in place, that chain of calls might never finish.5 There is also something happening underneath each of those calls. Each new function call gets added to the top of what programmers call the call stack.4 So with the definition and the stopping rule in place, where does recursion actually fit?

It tends to suit problems that can be split into smaller problems resembling the original one.3 Each smaller piece looks like a reduced version of the whole, which mirrors the nesting boxes from earlier. Recursive solutions also serve as a foundation for two families of algorithms, known as Dynamic Programming and Divide and Conquer.5 Within recursion itself, one particular arrangement has earned a separate label.

When the recursive call is the final statement in the algorithm, the pattern is called tail recursion.1 For all its strengths, recursion carries some limits worth understanding. It is a powerful technique, yet a version written without care can take up a large amount of memory.3 It also does not deliver any speed advantage.4

In other words, a program does not run faster simply because it was written recursively.4 Those two points place recursion in a clear light. It is a way of expressing a problem, built on a function that calls itself and a base case that tells it when to halt.2,4 The care goes into how it is written, especially around memory.3

Looking back, the opening puzzle about instructions that send the reader back to the beginning has a tidy resolution. The base case answers the simplest form of the problem, and once a recursive function reaches it, the function stops calling itself, which is what prevents an endless loop.1,4 Recursion is a technique, borrowed from mathematics, in which a function calls itself on smaller, similar problems until it reaches a base case that stops the calls.1,2,3,4

Quick quiz

Lock it in: pick an answer to see if you got it.

1. What is the purpose of the base case in a recursive function?

Answer: It stops the function from calling itself, preventing infinite loops. The base case is the point where a recursive function stops calling itself, which prevents infinite loops.

2. According to the lesson, what does using recursion do for a program's performance?

Answer: It brings no performance benefit. The lesson states that writing a program recursively brings no performance benefit; it does not run faster simply because it is recursive.

Sources

Every fact in this Gist is checked against these sources before it is published.

  1. Programming - Recursion
  2. The Recursion Explanation I Wish I Had Read As A Self- ...
  3. What is Recursion?
  4. How Recursion Works — Explained with Flowcharts and a Video
  5. Introduction to Recursion - GeeksforGeeks
  6. In plain English, what is recursion?

Get a new Gist every day

1,000+ Gists on 37 topics in the GistWise app: a Gist of the Day every morning, quizzes, streaks and offline listening.

Get the app

More Programming & Software

See all Programming & Software Gists →