# Ok. sorry to bother you guys with a newb question.

• 07-16-2003
arnis
Ok. sorry to bother you guys with a newb question.
Ok ive just started learning c++. Acctualy i tried to start for like a year ago but I didnt have enough time to learn it.
Anyway. here is my problem.

"Write a program that asks for a number and a power. Write a recursive function that takes the number to the power. Thus, if the number is 2 and the power is 4, the function will return 16"

That is an exercise from my book.

So far i have this
Code:

```#include <iostream> #include <stdlib.h> float myFunc(unsigned short int x, unsigned short int y); int main() { unsigned short int x = 0; unsigned short int y = 0; float z; std::cout<<"Write a exponent: "; std::cin>>x; std::cout<<"\nNow write a number: "; std::cin>>y; z = myFunc(x,y); std::cout<<"\n the answer is:" << z; system("pause"); return 0; } float myFunc(unsigned short int x, unsigned short int y) {   }```

I don't have a clue of what to put in my function. everytime i try it just ends up with some sort of a loop.
Don't laugh, i just started reading this book a couple of hours ago. And this recursive functions is hard for me to understand.
• 07-16-2003
arnis
Ok. I tried some more. This is what i got.

Code:

```#include <iostream> #include <stdlib.h> float sum; int count = 1; float myFunc(unsigned short int x, unsigned short int y); int main() { unsigned short int x = 0; unsigned short int y = 0; float z; std::cout<<"Write a exponent: "; std::cin>>x; std::cout<<"\nNow write the number: "; std::cin>>y; sum = y; z = myFunc(x,y); std::cout<<"\n the answer is:" << z; system("pause"); } float myFunc(unsigned short int x, unsigned short int y) { if(count==x) return sum; else{ count++; sum = sum * y; return(myFunc(x,y)); } }```
It works. But it just seams so , i dont know, uncorrect. I seams like i use a method that is not what the author of my book intended.
• 07-16-2003
JaWiB
Yes, you want to avoid using global variables when possible. The method I came up with involves passing x as two of the parameters of "myFunc" , where x1^y. The second x2 would change when the function recurses...ok maybe that doesn't make much sense so:

Code:

```float power(float x1,float x2,int y)   {     if (y>1)         power(x1,(x2*x1),(y-1));     else       return x2;   }```
• 07-16-2003
Eibro
Code:

```int power(int base, int exp) {     if (exp == 0)         return 1;     return (power(base, exp - 1) * base); }```
• 07-16-2003
JaWiB
I knew there was a better way...:o
• 07-16-2003
Zach L.
What are you guys talking about.
Code:

```template<int x, unsigned int y> struct power {   static const int val = x * power<x, y - 1>::val; }; template<int x> struct power<x, 0> {   static const int val = 1; };```
;)
• 07-17-2003
CornedBee
Zach, that is only for compile time.
• 07-17-2003
laasunde
This should work,
Code:

```#include <iostream> #include <stdlib.h> float myFunc(unsigned int, unsigned int); int main() {       unsigned short int exponent = 0;       unsigned short int number = 0;       float z;       std::cout<<"Write a exponent: ";       std::cin>>exponent;       std::cout<<"\nNow write a number: ";       std::cin>>number;       z = myFunc(exponent, number);       std::cout<<"\nThe answer is : " << z << endl << endl;       system("PAUSE");       return 0; } float myFunc(unsigned int exp, unsigned int num) {       if (exp > 0)         return (float) (num * myFunc( --exp, num) );       return 1; }```
• 07-17-2003
CornedBee
Why does the function return a float anyway?

Eibro's code is still the best.
• 07-17-2003
arnis
Thanks for all the replies. I will analyze this carefully now.

" CornedBee: Why does the function return a float anyway?"

I just thought that would be the best way. Because this numbers usualy gets very big.

example: 5^11 = very high number.
And im not sure how high an int can go.
As I stated earlier in this thread, im a newbie, but thats what i think.
• 07-17-2003
CornedBee
int can go up to 2 billion, unsigned int to 4 billion (on 32-bit systems anyway).

To be more specific, the highest possible value of int is
2^31 - 1
and of unsigned int
2^32 - 1
• 07-17-2003
arnis
oh thanks. thats good to know
• 07-17-2003
Zach L.
Quote:

Originally posted by CornedBee
Zach, that is only for compile time.
Yes, I know... It was really just a joke.