Recursion: A Programming Technique You Should Know
Learn recursion from easy-to-understand examples!
Recursion is ubiquitous in the programming world. In its simplest form, recursion is when a function calls itself from within its own code. When I was just starting out as a programmer, I found recursion rather difficult to understand. I kept wondering “Why on earth would we need to write a function that calls itself?”. The answer became a lot clearer when I started working through some examples.
This is what we will accomplish together in this story! We will learn recursion by looking at simple examples that demonstrate the usefulness of this programming technique. While it might seem odd to you at first that a function calls itself, it turns out this kind of programming technique appears everywhere in the real world.
There are a few crucial things to keep in mind when it comes to recursion. For one, a recursive function must actually terminate and return at some point. This is called the breaking condition. This prevents infinite loops from happening and the program from crashing so that the computer does not run out of memory trying to keep track of all the function calls.