• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

a problem of function recursion

I'm primer, I didn't know well about executive process of function recursion .for example

/* a recursive function instance */
int RecuFun(int n)
{
    int f1,f2;
     
    if ( n ==1 || n == 0 )
            return 1;
    else {
            f1 = RecuFun(n - 1);
            f2 = RecuFun(n - 2);
             
            return f1 + f2;

     }
}

main()
{
     int num =  5;
     int result;

     result = RecuFun( num );
     printf("%d\n",result);
}


running result :
8

but i am confused in else sentence section. please give a explanation thanks :)
0
China_Ark
Asked:
China_Ark
1 Solution
 
pjknibbsCommented:
I'm not sure what your difficulty is. Basically, the code in the "ELSE" section simply calls the recursive function again with different values--the IF statement is there to make sure the recursion ends at some point, because if the function ALWAYS called itself you'd get into an infinite loop and would crash with a stack space error.

Basically, the above code calls RecuFun(5). RecuFun(5) returns the value of RecuFun(4) + RecuFun(3). RecuFun(3) returns the value of RecuFun(2) + RecuFun(1). RecuFun(1) returns 1, ending the recursion down that branch. (Note I've missed one of the recursive calls at each level here).

It's perhaps easier to understand if you only have one recursive call in the function, e.g.

int factorial(int n)
{
  if (n == 1)
    return 1;
  else
    return n * factorial(n - 1);
}

This function calculates a factorial (e.g. factorial(6) = 6*5*4*3*2*1).
0
 
cincin77Commented:
an implementation of fibonacci numbers
what exactly did not you understand?
do you know about fibonacci numbers?
0
 
China_ArkAuthor Commented:
thanks to pjknibbs and cincin77.

Actually,it 's fibonacci function,the blow is its revise.
but still not efficent.
long RecuFun(int n)
{
        long f1,f2;
        if ( n ==1 || n == 0 )
            return 1;
        else {
        f1 = RecuFun(n - 1);
        f2 = RecuFun(n - 2);

        return f1 + f2;
        }
}

main()
{
        int num;
        long result;

        for( num = 0;num <=40;num++) {
            result = RecuFun( num );          
              printf("%10ld",result);
            if ( (num + 1) % 4 == 0 )
                    printf("\n");
        }
}
I don't see that as pjknibbs say, the main functin calls
RecuFun(5).
RecuFun(5) = RecuFun(4) + RecuFun(3)
_______________|            |
|  ________________________|
| |  
| |__> RecuFun(3) = RecuFun(2) + RecuFun(1);
|       ______________|
|       |__> RecuFun(2) = RecuFun(1) + RecuFun(0).
|____> RecuFun(4) = RecuFun(3) + RecuFun(2);    
     _________________|            |
     |  __________________________|
     |  |__> RecuFun(2) = RecuFun(1) + RecuFun(0);        
     |_____> RecuFun(3) = RecuFun(2)+ RecuFun(1);
                 ____________|      
                |__>RecuFun(2) = RecuFun(1) + RecuFun(0);
     
RecuFun(1) returns 1, ending the recursion
down that branch.

all right? :) ok. thanks to pjknibbs and cincin77 for giving me some good word.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
China_ArkAuthor Commented:
thanks to pjknibbs and cincin77.

Actually,it 's fibonacci function,the blow is its revise.
but still not efficent.
long RecuFun(int n)
{
        long f1,f2;
        if ( n ==1 || n == 0 )
            return 1;
        else {
        f1 = RecuFun(n - 1);
        f2 = RecuFun(n - 2);

        return f1 + f2;
        }
}

main()
{
        int num;
        long result;

        for( num = 0;num <=40;num++) {
            result = RecuFun( num );          
              printf("%10ld",result);
            if ( (num + 1) % 4 == 0 )
                    printf("\n");
        }
}
I don't see that as pjknibbs say, the main functin calls
RecuFun(5).
RecuFun(5) = RecuFun(4) + RecuFun(3)
_______________|       |
|  ________________________|
| |  
| |__> RecuFun(3) = RecuFun(2) + RecuFun(1);
|       ______________|
|       |__> RecuFun(2) = RecuFun(1) + RecuFun(0).
|____> RecuFun(4) = RecuFun(3) + RecuFun(2);    
     _________________|       |
     |  __________________________|
     |  |__> RecuFun(2) = RecuFun(1) + RecuFun(0);        
     |_____> RecuFun(3) = RecuFun(2)+ RecuFun(1);
                 ____________|      
                |__>RecuFun(2) = RecuFun(1) + RecuFun(0);
     
RecuFun(1) & ReuFun(0) returns 1, ending the recursion
down that branch.

all right? :) ok. thanks to pjknibbs and cincin77 for giving me some good word.
0
 
jonninCommented:
recursion is not generally efficient. The extra function calls cost more if nothing else does, and there is danger of blowing the stack on larger problems. Write it iteratively, if that is too slow create a lookup table of the numbers (1 operation to get a value, no loops or computations). Of course you will have to make the table large enough for whatever problem you are solving, with notes on when the table runs out...



(you can use the output of the slow function to create the values for the table, since you have it)

to speed it up as it is, store in a static array variable the computations you have already done (cache) and get them instead of recomputing ...

0
 
Kent OlsenData Warehouse Architect / DBACommented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept pjknibbs' comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now