Recursion, Recursions, Recursions, how cruel art thou.....j/k

This is a discussion on Recursion, Recursions, Recursions, how cruel art thou.....j/k within the C++ Programming forums, part of the General Programming Boards category; #include <iostream> // objective: to get a number and then a power and return answer using namespace std; typedef unsigned ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Recursion, Recursions, Recursions, how cruel art thou.....j/k

    #include <iostream>
    // objective: to get a number and then a power and return answer
    using namespace std;
    typedef unsigned short USHORT;
    typedef unsigned long ULONG;
    ULONG GetPower (USHORT number, USHORT power);
    int main()
    {
    USHORT number, power;
    double answer;
    cout<<"Enter a number:";
    cin>>number;
    cout<<"To what power?";
    cin>>power;
    answer=GetPower(number, power);
    cout<<number<<"to the"<<power<<"th power is"<< answer<<endl;
    int x;
    cin>>x;
    return 0;
    }
    ULONG GetPower (USHORT number, USHORT power)

    {
    if (power==1)
    return number;
    else
    return (number*GetPower (number, power -1 ));// I am not sure at what
    // this function is saying here
    /* I understand that the number is being multiplied by
    GetPower but I just don't understand what the GetPower function exactly does if you multiply number times power -1 or what, because I really don't understand where the definition for this one is at. Please help
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    /* I understand that the number is being multiplied by
    GetPower but I just don't understand what the GetPower function exactly does if you multiply number times power -1 or what, because I really don't understand where the definition for this one is at. Please help */

    functions are resolved by the type of argument that is passed, not the value of the argument. The same function gets called for getPower(2 , 3) as for getPower(2 ,5 ) as for getPower(4, 5).

    Running though a call of getPower(2,3).
    Code:
    getPower(2,3)
    returning 2 * getPower(2,2) 
    
         getPower(2,2)
         returning 2 * getPower(2,1)
    
              getPower(2,1)
              returning 2
    
         Back in getPower(2,2), knows getPower(2,1) is 2
         returning 2 * getPower(2,1), or 4
    
    Back in getPower(2,3), knows getPower(2,2) is 4
    returning 2 * getPower(2,2), or 8
    See the stack effect? The indent gets bigger and bigger until it reaches the base case (pow = 1), at which case it starts shrinking until the function is resolved.
    Last edited by SilentStrike; 11-17-2001 at 09:04 AM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Great explanation man

    If we wanted to change the base case all we would have had to do is change the condition on the if statement am I rignt?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Yeah, changing that if would change the base condition.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21