recursion

when i use this code i get answer that is always a raised to b+1.
why is that? when i put else statement in my power function i get the right answer? pls explain

#include <iostream>

using namespace std;
int power(int a,int b)
{
if (b>0)
{
return (a*power(a,(b-1)));
}

}

int main()
{int a,b;
cout<<"for a raised to b";
cin>>a>>b;
cout<<power(a,b);

}

Doesn't your compiler complain?

If (b>0) isn't true ... then your function doesn't return anything.
Registered users can post here. Sign in or register to post.