Between week four and week seven, CSC148 focused on recursion. Beforehand, I had no experience with recursive functions and only heard simple descriptions of it in 108 and 165. Back in week four, I read the assigned reading to get a basic understanding of what was to come in the following weeks. It gave a helpful definition of the term, saying that it means "“defining something in terms of itself” usually at some
smaller scale, perhaps multiple times, to achieve your objective." We can deduce that a recursive function calls itself within its body until it reaches its base case to achieve the correct result. It's basically when an active function uses itself over and over again to return an end.
For instance, in one of its simplest applications, recursion can be used on a nested list to return information about it, such as the amount of items within it, the sum of integers within it, its depth, among the infinite number of other possibilities. My first real challenge with recursion was during step 5 of A1, where you had to move all the cheese from the first stool to the last in a minimal amount of moves. Although it took a really long time to figure out, I was very satisfied with the fact that I was able to accomplish it. The tree emphasized during week six was a key example of recursive structures. It presented the use of recursion and object-oriented programming* together.
I believe that the advantage of recursion is that it allows us to write fast and optimized code, whereas writing for an assignment/exercise without recursion may lead to a longer runtime. Plus, we can even use recursion for abstract data types, which proved to be useful in week seven's lab.
Recursion is definitely more challenging that object-oriented programming. While OOP was just about writing simple classes and their methods, recursion makes us thinking deeper about how we want to achieve results and if there are faster ways to do it. Although it's taking longer for it to sink in, its practicality is motivating me to keep trying.
*Object Oriented Programming
No comments:
Post a Comment