> Stack Free Recursion
> Otto J. A. Smith
> Sept 29, 1995
Sorry to say this, but there is *no* such thing as stackless
recursion, nor stackless function call at all. This is because
when some function is called, the return address is saved
in the *stack*.
> /* STACKLESS FACTORIAL */
>
>int x;
>.
>. //Set x to the value you wish to find the
>facto
>rial of.
>.
>int factorial() //Pass x as Global No data Stack
>{
> int prod;
Local variables in a function are stored in -- guess where -- stack.
> x = x-1; //A function
> if(x+1 < = 0) prod = 1;
> else prod = (x+1) * factorial(); //The recursive call
The result for x+1 is calculated in register, which is then saved
into *stack* before calling the function again (or maybe just variable
x is saved into stack, but something in any case is saved into stack).
> x = x+1; //A function inverse
>return prod;
>}
The thing about what you were talking was not *stackless* recursion,
but in fact, *parameterless* recursion. The parameters are passed
in GCC using stack, which is avoided this way but they could be passed
in registers *or* as global variables too. But stack is needed anyway.
--
| Tuukka Toivonen <[EMAIL PROTECTED]> [PGP public key
| Homepage: http://www.ee.oulu.fi/~tuukkat/ available]
| Try also finger -l [EMAIL PROTECTED]
| Studying information engineering at the University of Oulu
+-----------------------------------------------------------