Menu
Coddy logo textTech

Introduction to Recursion

Part of the Logic & Flow section of Coddy's Dart journey — lesson 47 of 65.

Recursion is a programming technique where a function calls itself to solve a problem. Think of it like looking into two mirrors facing each other - each reflection contains a smaller version of the same image, creating an infinite loop until the reflections become too small to see.

Every recursive function needs two essential components to work properly. The base case is the condition that stops the recursion - it's like the point where the mirror reflections become too small to matter. Without a base case, your function would call itself forever, eventually crashing your program.

The recursive step is where the function calls itself with a modified version of the original problem. Each time the function calls itself, it should be working on a smaller or simpler version of the problem, gradually moving toward the base case.

Imagine you're looking for your keys in a stack of papers. You could check the top paper, and if your keys aren't there, you'd do the same thing with the remaining stack (which is now one paper smaller). You'd keep repeating this process until either you find your keys or there are no more papers left to check.

This approach of breaking down a big problem into smaller, identical problems is what makes recursion so powerful for certain types of programming challenges.

Cheat sheet

Recursion is a programming technique where a function calls itself to solve a problem by breaking it down into smaller, identical problems.

Every recursive function needs two essential components:

Base case: The condition that stops the recursion to prevent infinite loops.

Recursive step: Where the function calls itself with a modified version of the original problem, gradually moving toward the base case.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow