Recursion

Hi all,

I'm new to the forums; hopefully this is the correct place to post this. I was hoping someone could shed some light on recursion. I understand what is happening in this function, but I'm trying to figure out how to print to the screen the value of n after it has been multiplied.

For example: if I use cout << "n: " << n << " power: " << power << "\n"; I can see the variable "power" decrementing by 1, but I don't see the variable "n" incrementing with its new value after it has been multiplied by n * n. Your help is greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;

typedef unsigned short int USHORT;
typedef unsigned long int ULONG;

ULONG Getpower(USHORT n, USHORT power);

int main()
{

    USHORT number, power;
    ULONG answer;

    cout << "Enter a number: " << endl;
    cin >> number;
    cout << "To what power: " << endl;
    cin >> power;
    cout << "\n";
    answer = Getpower(number, power);
    cout << "\n";
    cout << number << " to the " << power << "th power is " << answer << endl;
    return 0;
}

ULONG Getpower(USHORT n, USHORT power)
{

    cout << "n: " << n << " power: " << power << "\n"; //Looking for variable "n" and "power" new values
    if(power == 1)
        {
            cout << "Inside if: " << power << "\n\n"; //Print to screen when i'm inside this condition
            return n;
        }
    else
        cout << "times:  " << (n * Getpower(n, power - 1)) << "  power: " << power << "\n\n"; //Trying to see the new value of "n" each time is multiplied n * n
        return (n * Getpower(n, power - 1));


}
What is most confusing about your function is that you call Getpower() in both lines 37 and 38. That means that your outputs are not going to happen in the order that you are expecting and will happen twice.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
ULONG Getpower(USHORT n, USHORT power)
{
    cout << "n: " << n << " power: " << power << "\n"; 
    if (power == 1)
    {
        cout << "Inside if: " << power << "\n\n";
        return n;
    }
    ULONG newVal = Getpower(n,power-1);
    cout << "times:  " << newVal  << "  power: " << power << "\n\n";
    return n * newVal;
}


Also, n will not increment. When you recursively call the function, you call Getpower(n,power-1). Therefore you've told power to decrement but you have not told n to increment. Where the incrementation occurs here is in the return statement. That's where you multiply n by the last value. In the snippet above you'll see the incrementation happen in newVal.
Good morning Stewbond,

Really appreciate the quick response! Thank you.

Your snippet helped me understand how n is multiplied by the last value.

I didn't expect power to increment (or at least appear to do so), from the example below do you know why power goes up?

Enter a number:
2
To what power:
4

Inside if: 1

times: 2 power: 2

times: 4 power: 3

times: 8 power: 4

2 to the 4th power is 16

Topic archived. No new replies allowed.