Recursion is one of the most famous concepts in computer science because it’s quite fun!
In this article, I will explain recursion and its different types and show you some famous examples.
Recursion is when a function calls itself, but the input will typically change. So, as the function is calling itself, it’s known as a recursive function.
You are essentially breaking down the problem into more minor problems, which are solved independently but added together step by step.
Pretty much every recursive function can be written in a loop format, but the recursive framing is often much more elegant!
A Russian Doll can be considered a recursion, as each doll contains another doll, then that one contains another etc.
Recursion could technically go on forever, but there are often some stopping criteria that prevent this. Otherwise, the computer will quickly run out of memory!
In general, a recursive function has two things:
- Base Case — Terminating scenario that does not require recursion.