What Is Recursion? Explained Simply
When a function solves a problem by calling itself on smaller pieces of the same problem.
What recursion is
Recursion is a technique in programming where a function solves a problem by calling itself on a smaller version of that same problem. Instead of a single big step, the function breaks the task into a smaller instance of itself, solves that, and builds up the answer. It is a way of expressing problems that have a repeating, self-similar structure. Recursion appears throughout computer science and is a fundamental concept worth understanding.
The base case
Every correct recursive function needs a base case, a condition that stops the recursion. Without one, the function would call itself forever, eventually crashing the program (a 'stack overflow'). The base case is the simplest version of the problem that can be answered directly, without further recursion. The recursive calls keep shrinking the problem toward that base case. Getting the base case right is the key to recursion that terminates correctly.
A simple example
A classic example is calculating a factorial, the product of all whole numbers up to some number. The factorial of 5 is 5 times the factorial of 4, which is 4 times the factorial of 3, and so on, down to the factorial of 1, which is simply 1 (the base case). Each step is defined in terms of a smaller step. This self-referential definition is exactly what recursion expresses naturally in code.
Recursion vs. loops
Many problems can be solved with either recursion or ordinary loops (iteration), and the two are closely related. Loops repeat using counters and conditions; recursion repeats through self-calls. Recursion can express certain problems, especially those with naturally nested or branching structure, more elegantly and readably. Loops are often more efficient in memory. Experienced programmers choose based on which makes the solution clearer for the problem at hand.
Where recursion shines
Recursion is especially natural for problems with a hierarchical or self-similar shape. Navigating tree structures, like folders within folders, or the elements of a web page, is a classic fit. So are 'divide and conquer' algorithms that split a problem into smaller subproblems, solve each, and combine the results. In these cases, a recursive solution often mirrors the structure of the problem itself, making the code clearer than a loop-based version.
Why it matters
Recursion is a foundational programming concept that, once it clicks, changes how you think about solving problems. It teaches you to break big tasks into smaller, self-similar pieces, a powerful mental model. Understanding recursion, and its essential base case, is a rite of passage in learning to program and unlocks elegant solutions to a whole class of problems that would be awkward to express any other way.
Related on Skillo
See also: What is an algorithm? Explained simply, Which programming language to learn first.
Sources
Published date reflects the original event date (2025-01-21). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.
Written by
Skillo Staff
0 Comments
Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.