Recursive Function.

Posts 1–11 of 11 · Page 1 of 1
Recursive Function.
I need to make a recursive function that returns a number to a entered power.
Such as 3 to the 3rd power is 27.

Here's my code so far. But I don't know the what'd id have to put in the return for it to call itself

[php]#include <iostream>

int PowerFunction(int number, int power); //prototype function

int main()
{
int number, power, result;
std::cout << "Enter number and power\n";
std::cin >> number >> power;

result = PowerFunction(number, power);
std::cout << "Result: \n" << result;
}

int PowerFunction(int number, int power) //defined function
{
return (what to put here?)
}
[/php]
So... if you entered 3 in the thingy, you want it to pop out as 27?

3 * 3 * 3?
And 4 * 4 * 4 * 4
???
to do a recursive just call her self...

like..

Code:
int main()
{
     cout << "Hello" << endl;
     main();
}
and that would get to a infinite loop... but i don't see how good is that in your program.. calling that function herself would result on a loop unless you had a if somewhere inside returning the value instead of her self.. idk about what you are trying to do.. honestly
Yes. v3ctor But It'd have to be a recursive function.

Quote Originally Posted by 'Bruno View Post
to do a recursive just call her self...

like..

Code:
int main()
{
     cout << "Hello" << endl;
     main();
}
and that would get to a infinite loop... but i don't see how good is that in your program.. calling that function herself would result on a loop unless you had a if somewhere inside returning the value instead of her self.. idk about what you are trying to do.. honestly
Based on the # entered for Power it would call itself that many times and multiply by lets say 4, that many times

I would use a If and else statement though. amiright
Quote Originally Posted by Kallisti View Post
Yes. v3ctor But It'd have to be a recursive function.



Based on the # entered for Power it would call itself that many times and multiply by lets say 4, that many times

I would use a If and else statement though. amiright
oh i got it..

Code:
int PowerFunction(int number, int power);  //prototype function 

int main() 
{ 
    int number, power, result; 
    std::cout << "Enter number and power\n"; 
    std::cin >> number >> power; 

    result = PowerFunction(number, power); 
    std::cout << "Result: \n" << result;
	system("pause");
} 

int PowerFunction(int number, int power) //defined function 
{
	power--;
	if(power!=0)
		number = number*PowerFunction(number,power);
	return number;
}
there
/thanked .

edit:doesn't work, but keep the thanks
Or maybe i said what it would do wrong.
Quote Originally Posted by Kallisti View Post
/thanked .

edit:doesn't work, but keep the thanks
Or maybe i said what it would do wrong.
you sure? it worked for me



2 = 2 (2^1)
2 * 2 = 4 (2^2)
2 * 2 * 2 = 8 (2^3)
2 * 2 * 2 * 2 = 16 (2^4)
2 * 2 * 2 * 2 * 2 = 32 (2^5)

or this is not what you want?
Quote Originally Posted by 'Bruno View Post
you sure? it worked for me



2 = 2 (2^1)
2 * 2 = 4 (2^2)
2 * 2 * 2 = 8 (2^3)
2 * 2 * 2 * 2 = 16 (2^4)
2 * 2 * 2 * 2 * 2 = 32 (2^5)

or this is not what you want?
It works now. For some reason it didnt work before. It works Perfectly now.
Quote Originally Posted by Kallisti View Post
I need to make a recursive function that returns a number to a entered power.
Such as 3 to the 3rd power is 27.

Here's my code so far. But I don't know the what'd id have to put in the return for it to call itself

[php]#include <iostream>

int PowerFunction(int number, int power); //prototype function

int main()
{
int number, power, result;
std::cout << "Enter number and power\n";
std::cin >> number >> power;

result = PowerFunction(number, power);
std::cout << "Result: \n" << result;
}

int PowerFunction(int number, int power) //defined function
{
return (what to put here?)
}
[/php]

Why don't you just do:

Code:
int PowerFunction(int number, int power) //defined function
{
	return number^power;
}
-SCHiM
Quote Originally Posted by schim View Post
Why don't you just do:

Code:
int PowerFunction(int number, int power) //defined function
{
	return number^power;
}
-SCHiM
probably because that isn't recursive

and probably because it is

pow(double,int)
Quote Originally Posted by schim View Post
Why don't you just do:

Code:
int PowerFunction(int number, int power) //defined function
{
	return number^power;
}
-SCHiM
the point of this is to use a recursive way.. -.-'
Posts 1–11 of 11 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us