Developer concepts

What is recursion?

A function that calls itself, each time on a smaller piece of the problem.

Some problems contain smaller copies of themselves. A folder holds folders, which hold more folders. Recursion is how code handles that.

What it is

Recursion is when a function calls itself, each time on a smaller piece of the problem.

How it works

Say you want to count every file in a folder. Count the files here, then call the same function on each subfolder, and add it all up. Every recursive function needs a base case, a point where it stops: a folder with no subfolders. Without one, it calls itself again and again until the program crashes.

Why it matters

Trees, folders, menus and lots of classic algorithms are naturally recursive, so the code ends up short and clear.

More from The Benmore Brief on YouTube

Put AI to work in your business.

Benmore's forward deployed engineers build and connect the software, from discovery to deployment.

Talk to us