Monday, 3 March 2014

Week 7

Recursion

Sometimes, a programmer's task will involving repeatedly executing the same process over and over (eg. verifying each item in a list, constructing a list). While there's the basic method of using loops, there is another more advanced method called recursion. Recursion, put simply, is a process triggering itself over and over again. A recursive function follows this concept; it calls itself over and over again until a certain condition is met. This condition is known as the base case and it is the case where the function stops calling itself. A base case is essential for a recursive function to be usable; without one, the function would keep calling itself indefinitely. Coding with loops will usually require quite a bit of code but a full functional recursive function can consist of only one line. This can make recursion preferable when working on large projects that require several functions and classes

So far though, I've found recursive function to be difficult to implement. While recursive functions can require little code, I find that their structures require a more clever, thoughtful, flexible design, otherwise there are many cases where the function could fail. In our first midterm, we had to write a recursive function to answer one of our questions. I found writing this function challenging. Overall, while a recursive function can be useful if implemented properly, I am not sure if it is worth the effort to do so

No comments:

Post a Comment