Thread: recurive function cannot understand one line plz have a look

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    28

    recurive function cannot understand one line plz have a look

    Code:
    //recursive factorial function
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    unsigned long factorial( unsigned long );
    
    int main()
    {
    
       for (int i = 1; i <= 10; i++ )
           cout << setw( 2 ) << i << "! = " << factorial( i ) << endl; // this part <<factorial(i)  calls function factorial with the parameter of i?
    
       return 0;
    
    }
    
    // recursive definition of function factorial
    unsigned long factorial( unsigned long number )
    {
       if ( number <= 1 )   // base case
          return 1;
       else                 // recursive case
          return number * factorial( number -1 );  //this part here i have problem also number* factorial(number-1 ,so what happens here factorial has no value? so why multiply by number? i'm lost please help.
    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    paramenter i, in that loop u set i to 1 and loop till it reaches 10. So whenever u have the call as factorial(i) ..... the present value of i is sent down to the function.


    okay here is a test case that will help you understand the function ( im guessing thats what ur having trouble with )

    lets assume i = 5 when the function is called.

    so it goes through the check and since 5 is greater than 1 it goes to the next step that is the recursice call.

    so now in reality things look like 5*factorial(4)
    it will go through the steps like till it hits 1, and will look something like this on the stack

    5*4*factorial(3) //second call
    5*4*3*factorial(2)//third call
    5*4**3*2*factorial(1)
    now it will return 1 and things will start to unwind and will go on multiplying.
    5*4*3*2*1 == 120

    and that is the answer....

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    so what happens here factorial has no value?
    factorial always returns a value, so you are multiplying number * the value returned by the function call. The fact that it is the same function is irrelevant, because each call is separate.
    Code:
    unsigned long square( unsigned long number )
    {
        return number * number;
    }
    
    unsigned long myFunc( unsigned long number )
    {
        return number * square( number - 1 );
    }
    The code in myFunc follows the same idea, multiply a number by the result of a function. Even though it is not recursive, it works the same as your recursive one.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Guys thank you so much i understand now how this works.
    Thanks alot !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM