Recommended Posts

I am always using recursiveness in my code, but I don't know how it 'really' works.
So I pulled out a simulated recursive factorial function. It was based on a big while loop with a fundemantal list holding data for each simulated call. Pretty much like the one generated by the call stack in an authentic c++ recursive function.
Yet an issue arises. I need to simulate something more complex, like a fibonacci recursive function with two calls per each new call. In this case my brain goes blank...
struct Body
{
// arguementdata ...
...
// List Pointers
Body *pNext, *pPrevious;
// Return Value
int ReturnValue; // Or any other type
// Constructors and Other stuff..
...
}
int factorial(int x)
{
Body Parent( x ); // initializes the struct with an
Body *Active = &Parent;
while( !Active->isDone )
{
if( ... ) // simplest case scenario ( ie x == 0 )
{
// Set the Return Value...
Active = Active->pBefore;
}
else if( Active->pNext ) // A value is returned from next node
{
// Delete next node and return based on the data given from that node
}
else if( Active->pNext == 0 )
{
// Create a new node and shift execution to that node
}
}
return Parent.ReturnValue;
}
[\code]
Hope that was self explanary enough...
The thing am asking for is the generalization of such built-in stack lists for function calls (even with multi calls)...
Please HELP!