|
|
My recursive nematode.
Recursion is a good tool to add to your
toolbox. You may not think of a reason to use it now, but years
in the future, you'll come upon a situation where recursion is a
perfect fit.
Recursive methods are based on two principles:
- Every recursive call must simplify the computation in some
way. In other words, as a method calls itself recursively, the
problem should be getting easier and smaller, not larger.
- There must be a terminal point of the recursion, which is
usually the point of the simplest computation. There must be a
special case to handle the simplest computation directly.
If you have a recursive call without simpler parameters, or if the
simple computation at the terminal point is missing, you will end up
with an infinite recursion. This will eventually cause your
computer, or the JVM, to run out of memory, and your program will crash!
I saw this sculpture, Sunyatta by Linda Howard, at the sculputre garden at New Orleans City Park. It reminds me of recursion. Unfortunately, the shot I took came out blurry, but you can see a better image at sculpture.net
Uses of Recursion
|