Monday, March 3, 2014

Recursion

The definition of recursion (from Google) is as follows: the repeated application of a recursive procedure or definition.

That doesn't explain too much, so what is it exactly, and what is its purpose? Is it something that provides us with another means to insult our peers?
While some may choose the path of impertinence, the role recursion plays in the world of coding is quite significant, almost vital to the entire process.

It is something that can be written in a simple line of code, yet capable of complex output. Who would have ever thought to call a function inside a function? Someone who probably didn't want to repeat several blocks of code. So then is recursion the product of laziness and apathy? Absolutely not. It is in fact, fundamental to computer science, providing a powerful means to control algorithms and approaches the possibility of self-automation robots similar to the ones in the film I, Robot. Just imagine what would happen if a computer possessed the potential to solve all of its own problems.

Since a recursive function calls upon itself infinitely many times, they require a 'base case', where the code is able to step out from a never-ending loop.

In this instance, the base case lies within "else x". Without the base case, the function would execute repeatedly, until eventually the program ran out of memory.

It's similar to two mirrors facing one another, and without a base case, you'd end up with infinite reflections reaching to the point where they become so minuscule, they are no longer visible.

At some point, you would need to step out.

It is interesting to ponder if the base case itself was a recursive function, for a recursive function defined within another recursive function. Being able to manipulate the concept of infinity is something far more sophisticated than making a 'yo mama' joke, but tracing recursion and trying to figure out how to implement it can often be difficult. Sometimes we just need to step back and crack a joke to realize what we were aiming for all along.

No comments:

Post a Comment