Sunday, March 2, 2014
Week 7: Recursion
Recursion occurs when a function calls itself inside its own body. At the start, the concept of recursion seems really confusing to most of the people including myself, but as you practice more problems involving recursion it becomes pretty simple. Being able to trace the code is one of the most important abilities for a computer scientist to have and tracing a recursive code was a difficult but interesting task. I found the key part of this type of tracing in re-reading the function over and over again as soon as you get to the part where the function calls itself until you get to a part where the condition isn't satisfied anymore which results in using the base case. So what is the base case? Generally, a recursive algorithm repeats itself until the point when conditions stated in the function are no more true. At this time, there exist another value which gets calculated and returned which is the simplest case that can occur in this function also known as the base case. Starting from the base case and moving forward to more complicated cases makes it much easier to understand how a recursive function works since you can re-use the previous results in order to calculate new values. So basically, we can conclude that recursion is breaking a complex problem into smaller and simpler cases until we reach the simplest case which becomes the beginning point for us to work our way back up to the original problem while taking easier and more understandable steps.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment