Okay, will somebody tell me what is wrong with this code?
Code:
#include <iostream.h>
typedef unsigned short int USHORT;
typedef unsigned long int ULONG;
ULONG powerFunc(USHORT, USHORT, USHORT);
int main()
{
USHORT num, orignum, pow; ULONG ans;
cout<<"Enter a number and a power:\t"; cin>>num;
cout<<"\t\t\t\t"; cin>>pow;
orignum=num;
ans=powerFunc(num, orignum, pow);
cout<<"\n\n"<<num<<" to the power of "<<pow<<" equals "<<ans<<".\n\n";
return 0;
}
ULONG powerFunc(USHORT num, USHORT orignum, USHORT pow)
{
cout<<"num="<<num<<" orignum="<<orignum<<" pow="<<pow<<"\n\n";
if (pow!=1)
{
num=num*orignum;
pow--;
powerFunc(num, orignum, pow);
}
cout<<"num to return="<<num;
return num;
}
Here is the output I get:
Code:
Enter a number and a power: 2
4
num=2 orignum=2 pow=4
num=4 orignum=2 pow=3
num=8 orignum=2 pow=2
num=16 orignum=2 pow=1
num to return=16num to return=16num to return=8num to return=4
2 to the power of 4 equals 4.
I've been messing with it for about an hour with no results. I am guessing it has something to do with local variables.