At first, I thought recursion was basically a while loop. As in it would keep calling the function until it had completed a loop through. Just like a while loop, if not implemented correctly, a recursive function will run infinitely. However, when it is controlled to run by using a base case, recursion works very well. The base case is virtually the function's "way out" and prevents it from just being circular by returning something.
Recursion, from what I can tell, is never completely necessary. There are ways to program without using it, mostly through for and while loops, yet sometimes it seems natural to use a recursive function because it can break up a problem into smaller pieces. Basically, the solution to the problem using a recursive function is dependent on the solution to smaller sub-parts of that problem as it moves closer to the base case.
Recursion is a good way to write code because it is more compact and is able to run through a larger problem without very complex code. Recursion divides and conquers a problem and returns the solution to all of those parts together. I definitely can now see the importance of recursion and how powerful a technique it is in Python.
No comments:
Post a Comment