Sunday, 8 February 2015

Tracing Recursion

Recursion has definitely been the biggest challenge so far in 148, and it definitely took me a while to wrap my brain around the concept. I started off having recursion explained to me by a friend in an upper year. As she started explaining, she started stumbling over her own words, and explaining the concept seemed to be confusing her (even though she is a master of recursion). You can appreciate how difficult it may be, if someone who understands it still has a hard time explaining it.
In simple terms, I would define a recursive function as one that calls upon itself within the body of the function. While this may not seem so difficult, it is of crucial importance to be able to learn how to trace a recursive function before you can actually start writing one. (I tried to do the latter first and my brain almost exploded).
The first couple of worksheets we got in class gave a good outline of the steps you would take to trace the function. We had to write out each step of the recursive call. After doing a couple of the same types of problems in our lab, it became easier to understand the concept of recursion. (As everyone says, practice makes perfect).  It is always necessary to break a big problem down into smaller steps. Once you master these elementary steps, you grasp a better understanding of the concept at hand. This is the way that I break it down for myself now:  The recursive function always has a base case, which is a condition that allows the function to stop recursion. If you know what the base case is, you know what the "end goal" of the function is, and what it should return for the simple problem. In terms of tracing, every call of the function is going to move closer to the base case. That means, every time the function is called it takes a new parameter which is pretty much a broken down version of the first parameter. What appeals to me is that even though it is a difficult concept, the code for a recursive function is very clean and concise (imagine having to rewrite the code over and over instead of just using recursion).
Even though I have tracing down, I am eager to see how I will do in terms of actually writing recursive functions.

No comments:

Post a Comment