Thursday, 27 February 2014
Recursion slog
Recursion can seem strange or confusing at first, but once it is understood, it can be quite useful. Recursion is essentially a repetition within a function where the function calls itself. This is generally used to break up the process of solving a problem into easier steps or for clarity purposes. It is used when the problem has a sub-problem which repeats the format of the problem in one way or another. Instead of typing up the same code again to solve this sub-problem, the function would be called within itself so that this sub-problem is treated as if it were the main problem. A good way to understand this process is tracing. Some examples of problems with similar formatted sub-problems are loops, nested lists, and Trees. Also, the Tower of Hanoi problem is a good example. The pattern here is that these examples have repetitions within themselves, similar to recursion, which is why recursion is so useful. To add to this, since recursion has the ability to infinitely repeat itself, an important piece to stopping this is a base case, where the function and or recursion continues until the base case is met.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment