DEV Community

Fernando B 🚀
Fernando B 🚀

Posted on

Recursion is Recursion is Recursion...

I took on the challenge of completing freecodecamp curriculum just to see the extent of my self-taught learning over the years, and is going well so far. Never did it occurred to me that I rarely use recursion functions, because well is easier to do a forloop. I tried to learn recursion before and the problem was that most people teaching start with the factorial example or some other complicated example. Mixing a math principle with a new programming principle isn't the best choice and sometimes is better to start with an easier example.

Credit: Freecodecamp Basic Javascript

What is recursion?

Recursion is when a function calls itself over and over until it hits the base case(s). A recursive function needs an exit statement, otherwise known as the base case because is the last time the recursive function gets called before returning the result. Another thing to be aware is that variables need to be passed down on each recursion, unlike in loops where you can keep track with local variables.

A recursive function works well with things that are normally done in a loop where there is a computation that is stepping up or down.

A few other examples of recursive functions are:

Thanks for reading, and I hope you learned something today!

Top comments (4)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

You could always Google it 😀
Google recursion

Collapse
 
gcgbarbosa profile image
George C. G. Barbosa

haha, that is a very nice easter egg

Collapse
 
pomfrit123 profile image
***

If you use return in you if condition you don't need an else, just a little tip :)

Collapse
 
thefern profile image
Fernando B 🚀

Thanks for the tip, I prefer if/else for beginner examples it shows the branching off a bit clearer for beginners.