How can I analyze a recursive source codes by hand?
For example, I have devised a technique for analyzing iterative source code by hand like this:
int fact(int n)
{
int f = 0;
int i = 0;
if (n<=1)
{
return 1;
}
f = 1;
i = 2;
for (i=2; i<=n ; i++)
{
f *= i;
}
return f;
}
---------------------------
i f new-f
---------------------------
---------------------------
---------------------------
For each ‘i’ I can analyze and calculate the values of old-f and new-f by hand and fill up the table to see if the routine is working correctly.
But how can I analyze recursive routines by hand?
int fact(int number) {
int temp;
if(number <= 1) return 1;
temp = number * fact(number - 1);
return temp;
}
Since recursion stores values on the stack you need to analyze it 2-way
1st pass is: do the recursion until the termination condition is reached.
2nd pass: collect the values until the stack is empty.