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)
You could always Google it ๐
![Google recursion](https://res.cloudinary.com/practicaldev/image/fetch/s--wzigqM_G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pgzkto469s9w9nwounfy.png)
haha, that is a very nice easter egg
If you use return in you if condition you don't need an else, just a little tip :)
Thanks for the tip, I prefer if/else for beginner examples it shows the branching off a bit clearer for beginners.