Friday, 4 March 2016

Week 7: Recursion..Is it really hard?

Recursion was the nightmare that many csc148 students dreaded since the beginning of the year. The amount of fear the word "Recursion" generated was actually quite hilarious. The negativity and the rumours about its difficulty can be justified in certain terms, but overall, the concept of recursion is actually quite straightforward when analysed under scrutiny. The key to understanding recursion is to approach the idea not as you would approach other ideas in programming, but thinking logically. Instead of trying to fit the notion into a familiar depiction of coding, we have to understand recursion by looking at a completely new slate.

You get the idea
I can write an entire essay on how recursive functions work, and how they can be utilized in everyday programming. But the point of this log is to share my thoughts and experiences about this topic, rather than trying to explain the topic itself. So to keep it simple and straightforward, a recursive function is basically any function that calls upon itself or exists in a potential cycle of function calls. Immediately, this can throw some people off. How can a function call itself if it's still running? Well that's the speciality of recursion. A recursive function infinitely loops upon itself until it comes across the desired result for whatever problem the function is trying to solve. For example: When trying to find the factorial of an integer (Let's assume 6), the recursive function keeps calling upon itself, starting from the integer 6, until it reaches 1, and upon each call, keeps multiplying the previous value obtained from the function. Factorials are just scratching the surface of the potential that recursion has in programming. From Fibonacci numbers to much more complicated mathematical problems, recursion can make programming quite efficient at times.

A helpful visualization of a recursive function finding the factorial of 5
I was first introduced to Recursion in high school, and to be honest, it took me an entire week to figure out how this miracle functioned. But once I started to notice the reasoning and efficiency behind recursion, I gained a whole new perspective in the world of coding. The utility of using a recursive function is actually quite immense, but finding out how to solve a problem using recursion is usually the hardest part. When trying to solve a problem in programming, our mind immediately starts thinking in terms of loops, classes and simple helper functions. But in order to utilize recursion, it is vital to approach the problem in a different way. It is necessary to find a pattern where the problem can be solved through an finite loop of the same series of events until it reaches a "base case". A base case is for a recursive function, what a "break" is, for an infinite loop. It ensures that instead of running infinitely, the function comes to a stop at a certain condition. When we look back at the factorial example. the "base case" for the factorial of 6 is when the value called into the function is 1. The moment it reaches 1, instead of calling itself again, the function finally returns the expected factorial value of 6. Thus, once we establish a base case for a problem that requires the occurrence of the same set of events multiple times with changing values called in each time, then a recursive function is the most effective choice for coding. Of course, this perspective of recursion is just the preliminary explanation, and we are learning much more complex ways to use recursive functions in our lectures. Recursive functions are always very tricky to come up with, and I always struggle with finding how to solve a problem using recursion. I have started practising recursion frequently in order to get more used to the idea of this different method of problem solving, and I can thankfully keep up with the lectures during class. I can't wait to learn more about recursion and get to utilize recursive functions in the next assignment or just programming in general. So to answer the question, recursion is not hard. It is just a different concept that might take a while for everyone to get used to, but understanding it is not a huge ordeal, just a little time-consuming at most. So if you're still struggling with recursion, just remember that you're not supposed to understand it instantly. Give it some time, and ponder over recursive functions before going to sleep, and sooner or later you will reach your Eureka moment.

 I will be back with another log for next week, but until then, Adios friends!

No comments:

Post a Comment