Here's output from a cygwin run:
$ ./a.exe
Enter a number: 2
To what power: 3
Entering function 2 3
Entering function 2 2
Entering function 2 1
4
Entering function 2 1
8
Entering function 2 2
Entering function 2 1
4
Entering function 2 1
2 to the 3th power is 8
|
Walkthrough:
Remember that each function call gets it's own stack, the variables may have the same name, but they are not the same.
Local variables have their storage allocated for them on the stack (this is why too deep recursion can lead to stack overflow)
These variables are accessed by moving the stack pointer up and down, so they're really not the same.
The very first time we call GetPower(2, 3), let's say we're at level 1 as soon as we enter the function
line 29: prints out the "Entering function 2, 3" line
x = power - 1 = 2
power isn't 1, so we skip to line 34
line 34: IN order to get the second operand for cout <<, we call GetPower(2, 2) -> using level 1 x i.e power - 1
Let's say we're now at level 2
line 29 and "Entering function 2,2"
level 2 x = power - 1 = 1
line 34 and once again call GetPower(2, 1) -> using level 2 x i.e. power - 1
Now we're at level 3
line 29 and "Entering function 2, 1"
level 3 x = power - 1 = 0 -> this is never used
This time power == 1, so we return 2
Now we're back at the cout << statement in level 2 (line 34)
so we multiply 2 * 2 and cout << 4 (line 34)
then for our return value we call GetPower(2, 1) -> using level 2 x (line 35)
This takes us again to level 3, where we're back to line 29 and "Entering function 2, 1"
This immediately returns us back to level 2, with a return value of 2, since power == 1
We multiply 2 * 2 and return 4 to level 1 (line 35)
in level 1 we're back at the cout << statement and we multiply 2 * 4 so we cout << 8 (line 34)
For the return value we call GetPower(2, 2) -> using level 1 x (line 35)
We're now at level 2 again
line 29 and "Entering function 2,2"
line 34 and once again call GetPower(2, 1) -> using level 2 x
Now we're back at level 3
line 29 and "Entering function 2, 1"
This time power == 1, so we return 2
Back at level 2 line 34 this again does cout << 4
This then calculates the return value by doing n * GetPower(2, 1) -> using level 2 x (line 35)
In level 3, line 29 and "Entering function 2, 1"
Immediately returns us back to level 2, with a return value of 2, since power == 1
We multiply 2 * 2 and return 4 to level 1 (line 35)
in level 1 we return 2 * 4 back to main (line 35)
It might help to look at it as a series of independent functions, instead of the same function being recursively called. Here's a simulation that does exactly the same as your code (for n = 2, and power = 3), except that instead of calling itself (recursion), it calls another function, which calls another function, etc.
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 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
typedef unsigned short USHORT;
typedef unsigned long ULONG;
static ULONG GetPowerLevel1 (USHORT n, ULONG power);
static ULONG GetPowerLevel2 (USHORT n, ULONG power);
static ULONG GetPowerLevel3 (USHORT n, ULONG power);
int main ()
{
USHORT number = 2, power = 3;
ULONG answer;
cout << "n == " << number << endl;
cout << "power == " << power << endl;
answer = GetPowerLevel1 (number, power);
cout << number << " to the " << power << "th power is " << answer << endl;
_getch();
return 0;
}
ULONG GetPowerLevel1 (USHORT n, ULONG power)
{
cout << "Entering function GetPowerLevel1 " << n << " " << power << endl;
if (power == 1)
return n;
else
cout << (n * GetPowerLevel2 (n, power - 1)) << endl;
return (n * GetPowerLevel2 (n, power - 1));
}
ULONG GetPowerLevel2 (USHORT n, ULONG power)
{
cout << "Entering function GetPowerLevel2 " << n << " " << power << endl;
if (power == 1)
return n;
else
cout << (n * GetPowerLevel3 (n, power - 1)) << endl;
return (n * GetPowerLevel3 (n, power - 1));
}
ULONG GetPowerLevel3 (USHORT n, ULONG power)
{
cout << "Entering function GetPowerLevel3 " << n << " " << power << endl;
return n;
}
|