Link to home
Start Free TrialLog in
Avatar of Koraxen
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?


int MyFunc(int x) {
 
	if (x <= 1) {
		return (1);
	} else {
		return (x * MyFunc(x - 2));	
	}
}

Open in new window

Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

Something like this?

#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;
}
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Koraxen
Koraxen

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.
>> I can easily throw this into the compiler and run it, and get 105.

Yes. And what's your question ?
Avatar of Koraxen

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 ?
Avatar of Koraxen

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)" ?
=============
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));
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial