Friday, March 4, 2016

Week 7 SLOG

A new topic discussed in CSC148 this week was recursion. Recursion is the idea of a function calling itself again. The widely used example to explain recursion is factorials.
Since n! can be divided into n * (n-1)(n-2)(n-3)...(1) which is equal to n*(n-1)!, repeatedly calling the factorial of (n-1) can be used to calculate the factorial. Through this example, I learned that recursion is convenient to use if the solution of a smaller part of the function can be found using the same algorithm as the original problem. Two things that came to mind after learning about this concept are matryoshka (Russian nesting) dolls and the Fibonacci sequence.
The most important part that programmers need to be very cautious when using recursion is the terminating condition. A terminating condition is always needed in using recursion because that condition enables the recursive function to come to an end. Without a proper terminating condition, the recursive function call will be made infinitely. When programming, errors that are raised notify the programmers to fix the program. However, when there is an infinite loop, the program goes on and on without any errors being raised.
Recursive functions usually consist of if, elif and else blocks. One of these blocks has to be the terminating condition which does not make any recursive calls. The most difficult part to me is writing the code for the block that makes several recursive subcalls. Writing the code in order to return the combined result is still confusing. However, I am planning to get used to this by getting more practice through the additional exercises in lab 6 this weekend. Even after grasping the concept and the basic structure of recursion, it was still difficult to imagine how a function calls itself and what result that brings to the body of the function. However, the tracing exercise of lab 6 was very helpful in understanding how recursion works. By going through the step by step tracing process, I could see how and when the function max_depth is called to calculate depth of the list. 

No comments:

Post a Comment