Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
Up to date
Published
4 min read

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

Recursion Explained In Simple Terms

Understanding recursion through real examples - why functions call themselves and when to use them

Most tutorials explain recursion using factorials or Fibonacci sequences. That’s nice for learning, but you won’t use those in real work. Let’s look at recursion through actual code you might write.

Think of recursion like reading through a thread of Slack comments. You see a message with replies, and those replies have their own replies, and it keeps going deeper. Each level of replies is the same thing - just messages and responses - but each level is dealing with a smaller piece of the conversation.

In programming, recursion works the same way. A function calls itself to handle a smaller version of the same task. Here’s a Slack comment system to see how recursion works.


This code would output:

That’s all recursion is - a function that calls itself to handle smaller pieces of data. In our example, it’s showing a comment, then showing all its replies, then their replies, and so on.

Every recursive function needs two crucial parts:

  1. Something it’s trying to do (like displaying a comment)
  2. A “base case” that tells it when to stop (when there are no more replies)

Think of it like this: if someone asks you to count all the comments in a Slack thread, you might say “Okay, I’ll count this comment (1), then count all the replies, then count their replies…” That’s recursion - breaking down a big task into smaller, similar tasks.

File Explorers and Directory Trees

The most common place you’ll use recursion is when dealing with files and folders. Think about Windows Explorer or Finder on Mac. They both need to show you files inside folders, which can have more folders inside them, and more folders inside those… you get the idea.

This function looks in a folder. If it finds a file, it adds it to the list. If it finds another folder, it looks inside that one too.

A Store Category Menu

The category menu in an online store works just like nested folders. Electronics contains Computers, which contains Laptops. Each category can hold more categories inside it.

Running this code shows:

The showCategories function looks at each category and then peers inside it for more categories.

It starts at “Store” and prints it. Inside Store it finds “Electronics”, prints that with some space to show it’s inside Store. This pattern continues - looking inside each category and printing what it finds - until it hits empty categories. The spaces at the start of each line show how deep the category sits in the menu.

Essentially, recursion is just a way to handle nested data - like comments with replies, or folders with more folders inside. Don’t overcomplicate it. If you can break down your problem into smaller, similar problems, recursion might be the right tool for the job.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Tech
4 min read

Chrome Is Beta Testing Built-In AI. Could This Kill a Lot of Startups?

The Power Play: Gemini Nano in Chrome

Aug 31, 2024
Read article
Tech
3 min read

Google is Killing Information Economics on the Internet

Google’s Gemini pulls summaries from websites and slaps them directly into the search results

Sep 11, 2024
Read article
Tech
3 min read

The Internet is Becoming an Ocean of LLM-Generated Junk

The internet’s full of content, but most of it is becoming junk. I’m talking about the stuff generated by Large Language Models (LLMs). These AI tools are cranking out endless articles, and the quality? It's bad—really bad.

Sep 9, 2024
Read article
Tech
4 min read

When Regex Goes Wrong

Issues and catastrophic failures caused by regex

Aug 29, 2024
Read article
Tech
10 min read

Amazon's Rise to Tech Titan: A Story of Relentless Innovation

How Jeff Bezos' 'Day 1' philosophy turned an online bookstore into a global powerhouse

Sep 30, 2024
Read article
Tech
5 min read

Can OSSPledge Fix Open Source Sustainability?

The Open Source Pledge aims to address open source sustainability challenges by encouraging companies to pay $2,000 per developer per year

Nov 17, 2024
Read article
Tech
5 min read

Understanding Agent2Agent (A2A): A Protocol for LLM Communication

An exploration of Google's new open protocol that enables different AI systems to exchange information and collaborate

Apr 13, 2025
Read article
Tech
9 min read

Secure Your Repositories: Prevent Credential Leaks with Gitleaks

Automate security flows and ensure your team follows security best practices

Aug 6, 2024
Read article
Tech
3 min read

Introducing the Legendary Programmer Hall of Fame

Meet the innovators who laid the foundation for modern computing. Their contributions span decades, creating the tools and concepts developers use every day.

Oct 29, 2024
Read article

Become a better engineer

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Many companies have a fixed annual stipend per engineer (e.g. $2,000) for use towards learning resources. If your company offers this stipend, you can forward them your invoices directly for reimbursement. By using my affiliate links, you support my work and get a discount at the same!


This article was originally published on https://www.trevorlasn.com/blog/recursion-explained. It was written by a human and polished using grammar tools for clarity.