Ah, recursion. Its a fairly hard concept to grasp onto at first; it seems counter intuitive to any programmer first starting out with it. How can a function or program call upon itself? Wouldn't that create an infinite loop? Contrary to what most beginners, including me, thought, recursive thinking in programming is actually quite useful; it saves on quite a bit of redundant code and allows for programs that would not normally run without recursion.
Recursion, in short, is when a program calls upon itself to solve smaller instances of a larger problem. A good example of this is a program that wants to add up all of the numbers in a list of lists. The program can call upon itself using an if statement while calling the function 'isinstance'. With a variable set to 0 already in place, this function will look into all of the instances of lists in the object the program was called on and add the numbers to the variable. Thus, it will return a cute little integer or floating point number without all the hassle of redundant code.
I'm still trying to wrap my head completely around the concept. I've spent quite a bit of time going through recursive functions and tracing how they go about accomplishing their tasks. While not quite in the clear of understanding, I'm starting to see the light!
The light from my monitor, at least.
No comments:
Post a Comment