DEV Community

Discussion on: Recursion Explained (with Examples)

 
aaronmccollum profile image
Aaron McCollum

True that! I've done free versions of Codecademy before - that was my first HTML course almost a year ago today. Ah the memories.

Thread Thread
 
wulymammoth profile image
David • Edited

Welcome to the world of programming, Aaron -- I used to hate recursion because it was not intuitive to me at all no matter who explained it. When I learned it there weren't such abundant resources. It took me years to come back around to learning it (all the while hoping no one around me would find out that I couldn't do it)

Here are a few things that may help with some lingering questions (e.g. when to use it?). Whenever you've a problem and the only thing that comes to mind as the answer the problem is to "enumerate" or "try all possibilities" or an "exhaustive search" is usually a signal for recursion, because the recursive parts are probably repetitive...

One thing that I was aware of, but wish someone had really forced me to understand was thinking about how to do recursion in terms of a decision tree aka recursive tree -- "in my current position, what are my options?"

With Fibonacci, we have two branches (i.e. two decisions) at EVERY (node) call with one exception (base case) when the value that comes into our function is 0 or 1

Drawing this out by hand is an exercise that has made it click for me. While I can mostly do this in my head now or in a comment in the code, if I can't determine what my decisions and exceptions are, then I cannot write the code

Thread Thread
 
aaronmccollum profile image
Aaron McCollum

Thanks David! I appreciate this. 😊