Yes, you are actually computing n!
there. One way to do it is the following:
#include <iostream>
#include <string>
int powerThroughRecusion(int n, int step) {
if (step < 1)
return 1;
return n * powerThroughRecusion(n, step - 1);
}
int main()
{
std::cout << powerThroughRecusion(4, 4);
}
You multiply by n, but a step will tell you how much multiplications to be done.
[edit] using a single parameter function
#include <iostream>
#include <string>
int powerThroughRecusionInternal(int n, int step) {
if (step < 1)
return 1;
return n * powerThroughRecusionInternal(n, step - 1);
}
int powerThroughRecusion(int n)
{
return powerThroughRecusionInternal(n, n);
}
int main()
{
std::cout << powerThroughRecusion(2) << "\n";
std::cout << powerThroughRecusion(4);
}