Understanding Recursion

Ian Wright
Nov 16, 2020
Photo by Kevin Ku on Unsplash

The basics of recursion is a function that calls itself until it doesn’t. Sounds simple and easy but it can get quite tricky. Recursion is made for solving problems that can be broken down into smaller, repetitive problems. It’s best used for working on problems that may have many branches attached.

FOR LOOP OR RECURSION?

I was once told when you can use a for loop you can use recursion, but when you use recursion you can’t always use a for loop. There is always a time and a place to use both respectively, either of these ways are helpful when it is best suited for them. Just don’t get too comfortable using just 1!

Photo by John Schnobrich on Unsplash

Using Recursion

First things first, we always need a base case! A base case is very important when using a recursive call, it basically will tell the function when to stop running. If there's no base case the function will keep running and looking for what it's supposed too and your computer won't like that! Once you have a base case go ahead and write some code so the function can find what you want! Using your recursive call can look many different ways but it will always be the function call inside of itself!

--

--