"Michael S. Davis" wrote:
> 
> On Thu, 22 Jun 2000, Charles Rezsonya wrote:
> 
> > i keep hearing recursion should be avoided whiled devin' up an app.  what
> > exactly does this mean?  don't call too many loops or repeated functions?
> > any extra details or specs on what and what not to do would be appriciated.
> > brief is ok =)
> 
> This has always puzzeled me also.  Recursion means a routine that calls
> itself.  Computing a factorial is a classic example.
> 
> But what confuses me is that a routine that recurses 3 times generally
> takes up no more stack than a routine that calls another routine, which
> calls a third routine.
> 
> It seems that the operative warning hers should be "don't nest
> routines" too deep rather than "don't use recursion".  Each time you
> call a new function, you place a lot of data on the stack AND the stack
> space is limited.
> 
> In fact, it is quite possible that a routine that recurses 4 times may
> use less stack that 3 separate routines that are nested.
> 
> Anyone care to explain why there is a warning against "recursion" rather
> than a warning against "nesting" of functions.
> 
> Thanks

Because typically a recursive function nests itself deeper than you calling
different functions.

For example, your program may have, lets say 100 functions.  So without
using recursion, you will never be more that 100 functions deep.  Really
you will be much less.

Now take a typical recursive function.  Lets say you write a recursive
function to calculate factorials.

Now lets say you want to calculate 200! Perfectly reasonable.  All
of a sudden you are recursing 200 levels deep.  So by making it
recursive, you are limiting the ranges you can calculate.

Whereas if you rewrote your algorithm to not be recursive but instead
do it in a loop, you can now calculate a much greater range.

Bman

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to