//Hi all ,can you explain why does this functon has two returns?
//How to read them? Is it like if/else?
int _tmain(int argc, _TCHAR* argv[])
{
int x, n;
int res;
int power(int, int);
cout << "Enter a number:";
cin >> x;
cout << "Enter a power:";
cin >> n;
res = power(x, n);
cout << "X^N is:" << res << endl;
return 0;
}
int power(int x, int n)
{
if (n == 0)
return 1;
if (n % 2 == 0)
return power(x*x, n / 2);
return power(x*x, n / 2)*x;
}
aluhnev
0
Junior Poster in Training
Recommended Answers
Jump to PostThe
power
function has three returns. The first is inside the firstif
, the second (which is wrongly indented) is inside the secondif
and the third is outside of anyif
. The third will only execute if none of the previousif
s were executed (just like the second will …
All 2 Replies
Reply to this topic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.