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

1. 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;
cout<<"Enter a number:";
cin>>number;
cout<<"To what power?";
cin>>power;
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
}

2. /* 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.

3. 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?

4. Yeah, changing that if would change the base condition.