Koraxen
asked on
Recursive function trace
Hey guys,
I have little experience with recursion and was wondering if someone could help me figure out how to trace it. I need to obtain its output by calling it with a particular parameter, such as 7, for example. Is this as simple as putting printf() statements or am I missing something big?
I have little experience with recursion and was wondering if someone could help me figure out how to trace it. I need to obtain its output by calling it with a particular parameter, such as 7, for example. Is this as simple as putting printf() statements or am I missing something big?
int MyFunc(int x) {
if (x <= 1) {
return (1);
} else {
return (x * MyFunc(x - 2));
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I understand. I think this might be a trick question though, because it seems too straightforward. Given that snippet of code, the actual question is:
What is the value of A, where "A = SpecialFunction (7)"
I can easily throw this into the compiler and run it, and get 105.
What is the value of A, where "A = SpecialFunction (7)"
I can easily throw this into the compiler and run it, and get 105.
>> I can easily throw this into the compiler and run it, and get 105.
Yes. And what's your question ?
Yes. And what's your question ?
ASKER
It just sounds like one of those questions that's too easy to be true. Either I'm expected to trace it by hand or there's some trick to it that I'm not seeing.
Is this an assignment ? If so, can you post the exact wording of it ?
ASKER
Yes.
=============
Consider the following function:
int MyFunc(int x) {
if (x <= 1) {
return (1);
} else {
return (x * MyFunc(x - 2));
}
}
What is the value of A where "A = MyFunc(7)" ?
=============
=============
Consider the following function:
int MyFunc(int x) {
if (x <= 1) {
return (1);
} else {
return (x * MyFunc(x - 2));
}
}
What is the value of A where "A = MyFunc(7)" ?
=============
Then you've already got the answer - you posted it earlier : 105.
If you need to add a printf to the code, then just do this :
printf("%d", MyFunc(7));
If you need to add a printf to the code, then just do this :
printf("%d", MyFunc(7));
Just for fun, you might add a counter and increment it upon entry and decrement it on exit. Include the counter in your printf(), and you can watch your recursion in action.
Working from peetm's version:
int MyFunc(int x)
{
int ret;
static counter=0;
counter++;
printf("(%d) In MyFunc x = %d\n", counter, x);
if (x <= 1) {
ret = 1;
} else {
int d = MyFunc(x - 2);
printf("(%d) Tail from 'else', x = %d d = %d\n", counter, x, d);
ret = (x * d);
}
counter--;
return ret;
}
int main(void)
{
printf("%d\n", MyFunc(10));
getchar();
return 0;
}
Bill
Working from peetm's version:
int MyFunc(int x)
{
int ret;
static counter=0;
counter++;
printf("(%d) In MyFunc x = %d\n", counter, x);
if (x <= 1) {
ret = 1;
} else {
int d = MyFunc(x - 2);
printf("(%d) Tail from 'else', x = %d d = %d\n", counter, x, d);
ret = (x * d);
}
counter--;
return ret;
}
int main(void)
{
printf("%d\n", MyFunc(10));
getchar();
return 0;
}
Bill
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#include<stdio.h>
int MyFunc(int x)
{
printf("In MyFunc x = %d\n", x);
if (x <= 1)
{
return 1;
}
else
{
int d = MyFunc(x - 2);
printf("Tail from 'else', x = %d d = %d\n", x, d);
return x * d;
}
}
int main(void)
{
printf("%d\n", MyFunc(10));
getchar();
return 0;
}