Recursion is: see
previous post on recursion. No really; In a nutshell that's
recursion! Recursion is having faith that a problem has already been
solved and letting it solve itself using that knowledge.
Ok, so there's a
little more to it than that but since I had already made a post
involving recursion, it seemed appropriate to define it that way.
Recursion is a very powerful tool in any programmer's toolbox.
When a recursive
function or method is executed there is generally a condition to be
met or a check to determine whether the recursive case or the base
case should be executed. If the recursive case is needed the function
calls it self, and stores the calls in memory. This continues until
all that's left is the base case. Then the base case is resolved.
Once this happens, all the previous recursive cases can then be
resolved with respect to that base case.
The reason that
this is so brilliant is because a lot of problems actually turn out
to be the same problem wrapped up within themselves many times. This
variety of problem can seem quite large and daunting when viewed as a
whole. However, the power of recursion allows for a very useful way
of modelling such a problem.
A common example
of recursion that many people will have seen is factorials in math.
Each time a factorial is increased, it becomes the product of the new
number and the previous factorial total. This is a recursive
structure because each factorial can be broken down in such a way.
At
first I struggled with the concept of recursion, but being
exposed to it repeatedly has greatly helped my comprehension. The
weekly labs in particular have been a great opportunity to learn
recursion with the help of peers and teaching
assistants.
No comments:
Post a Comment