When you call a procedure you use up a bit of the program's stack. The stack is used to keep track of procedures in a program which haven't finished, and real problems can arise when the stack space runs out. Normally, the amount of stack available to each program is sufficient, since the E compiler handles all the fiddly bits quite well. However, programs which use a lot of recursion can quite easily run out of stack.
For example, the
fact_rec(10) will need enough stack for ten calls of
fact_rec, nine of which are recursively called.
This is because each call does not finish until the return value has been computed, so all recursive calls up to
fact_rec(1) need to be kept on the stack until
fact_rec(1) returns one.
Then each procedure will be taken off the stack as they finish.
If you try to compute
fact_rec(40000), not only will this take a long time, but it will probably run out of stack space.
When it does run out of stack, the machine will probably crash or do other weird things.
The iterative version,
fact_iter does not have these problems, since it only takes one procedure call to calculate a factorial using this function.
If there is the possibility of running out of stack space you can use the
FreeStack (built-in) function call (see 11.3.5 System support functions).
This returns the amount of free stack space.
If it drops below about 1KB then you might like to stop the recursion or whatever else is using up the stack.
Also, you can specify amount of stack your program gets (and override what the compiler might decide is appropriate) using the
OPT STACK option.
See the Reference Manual for more details on E's stack organisation.
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.