Monday, 3 March 2014

Week 5 SLOG - Recursion

My task for today is to demonstrate a little bit of what I have learned about recursion so far in my programming course. Recursion has been a big topic in the course so far, and I believe it will continue to be, so it's very important that I understand it and can put it into practice.

Recursion is when a function is called within itself. Sometimes when you're using a function to code, the task being performed by the function can be broken up into smaller instances of the same task. By figuring out how to complete these smaller versions of the task, you can use them to complete a bigger version of the task which contains the smaller versions. By 'plugging in' the smaller versions of the task to the bigger version, you are using recursion. Any time that you can break up a problem into smaller, easier versions of the same problem is probably a good time to use recursion. When you are writing recursive code, you need to start with the base case. This is the case which does not involve any recursion, and all following cases will build off of it (the smallest version of the task). It is very important that you have a base case, otherwise your program will probably go into an infinite loop.

I personally feel that recursion is a very useful tool in computer science. The more that we look at it in class, the more I start to understand how integral it is to writing code. It makes coding certain problems much easier and much shorter when you can understand how it is done. It can cut out a lot of repeated code, because you're calling a function within a function, instead of writing out more code in place of the inner call to the function. Although it seems complicated at first (or at least, it did to me), it actually has a very intuitive feel to it once you get going. I've found that there's actually a risk of overthinking a recursion problem; it can be better to keep your code pretty much as simple as the problem itself.

I also think that it's important not to forget the basics though. There are times when a problem could be solved through recursion or through another way, such as a for loop, and just because you can use recursion doesn't mean you should. Recursion can take a lot of steps to complete things, so there are definitely times when a straightforward approach would be much better than a nested approach like recursion. For instance, when trying to find the number of items in a nested list, it would be better to have a variable that keeps track of the number of items as you add to it, rather than use recursion to look at all of the elements of the nested list each time you wanted to know the number of items.

Overall, I find recursion to be very interesting and useful, and I'm looking forward to learning more about it.