Starting your Project at the End
Phil Hadviger
Principal Site Reliability Engineer @ GLG#
It started with recursion for meOne of my first computer science class lessons that stuck with me the longest was when we were introduced to recursion. Focus on the stopping case!
With iteration, the approach was generally simple: pick a starting point, and stop at the end. For example, use a for loop to add up the numbers 1 through n. Simple, just declare a sum variable have each step of the loop increment the sum until you reach 1.
Recursion was a different beast, because we didn't really have sum as a variable and had to add the results of more function calls until we reach a stopping case, at which point we did something completely different and returned just a number.
Since we don't have the luxury of the sum
variable that is part of the iteration, the stopping case in the recursive example is really important to think about if you want the results to come back the same as the iterative version.