Programming Concepts Tutorial

Learn the fundamental concepts of computer programming

Recursion

Recursion

Recursion occurs when a function calls itself from within its own code.

It is a powerful tool for breaking down complex problems into simpler ones, like traversing a file system tree or calculating factorials.

The Base Case

Every recursive function MUST have a base case—a condition that stops the recursion. Without a base case, the function will call itself infinitely, causing an "infinite loop" or "stack overflow".

Try It Yourself

Edit the code below and click Run to see the result live.

Source Code
Result
Topics