Tuesday, March 3, 2015

Tuesday, March 3rd, 2015

So I talked about using recursion to go through trees 2 posts ago, but I have not talked about recursion specifically. So in this post, I will talk about the deceivingly beautiful concept of recursion.

Three weeks ago, I was not sure how I felt about recursion. I was very uncomfortable with the concept of it and was not sure if I would ever learn to become accustomed to it. However, as the weeks went by, I began to realize how useful it can be for solving problems. I mean, creating a function that calls itself in the body is honestly mind-blowing. I like to think of it as function-ception (a function inside of a function, for those who have not seen the amazing film Inception (2010)).

A recursive function is a function that calls itself. Any function that uses recursion can also be created by using iteration. However, recursion makes the function concise and better looking, where as iteration can make the function long, complicated, and ugly for other computer scientists to read. In order to ensure that recursion will work, you must figure out a base case (for example, k = 0) and find out if you can solve the base case of your problem. If it works, you can increment the base case value (ie. take k = 0 + 1, so k = 1) and see if it still works. Eventually, your function will work for any case (ie. k >= 0) and you can assume it will work for all cases. Thus, recursion is your friend here. This concept of seeing if the function works for a base case, and then incrementing is called induction. In short, creating a function that calls itself, (ie. a recursive function) makes solving problems in computer science a whole lot easier.

We have been using recursion for the past 3 weeks in class and in labs which have both been very helpful for my understanding. Writing recursive code can get a little tricky for myself but working on the lab exercises with my partner has also been helpful. Unfortunately, labs may get cancelled this week due to the TAs strike that is going at this very moment. Hopefully it ends soon so that I can continue going to labs and develop my understand of recursion and computer science!

No comments:

Post a Comment