# Thread: Recursive Programming -- Power of function

1. ## Recursive Programming -- Power of function

I am using Stephen Prata's C Primer Plus (5th Edition) to teach myself C Programming. I am not a student (I wish), and am seeing myself stuck!

(bear with me, this is my first post so I hope I get the formatting right)

I have this code:

Code:
```#include <stdio.h>

double power(double a, int b);  /* ANSI prototype */

int main(void)
{
double x, xpow;
int n;

printf("Enter a number and the integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf("%lf%d", &x, &n) == 2)
{
xpow = power(x,n);       /* function call           */
printf("%.3g to the power %d is %.5g\n", x, n, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}

// test runs:
// 1 & 1 == 1
// 5 & 5 == 3125
// 6 & 7 == 2.7994e+005

double power(double a, int b)
{
double pow = 1.0;
int i;

if ((b == 0) && (a == 0)) pow = 1.0;
else if (a == 0) pow = 0.0;
else if (b > 0)
for(i = 1; i <= b; i++)
pow *= a;
else    /* b < 0 */
pow = 1.0 / power(a, - b);
return pow;
}```
I am learning Recursion and the books suggest transforming the above using a recursive approach; I just cant get my head around it. any suggestions would be highly appreciated.

2. You should concentrate on the loop

Code:
``` for(i = 1; i <= b; i++)
pow *= a;```
on each iteration it multiplys the old value to a

instead of the loop you can call power recursively to calculate the previous value, multiply it with a and return result

3. would you mind showing me in code?

OOPS -- noticed I posted this in C++ and not the C section. sorry

4. First, format this so that me/you/we know what we're looking at. That, and get your original in better shape before going for a recursive.
Code:
```/*Decide between tabs/spaces, and stick to it.*/
double power(double a, int b)
{
double pow = 1.0;
int i;

/* This if() has no effect, pow is already 1.0...*/
if((b == 0) && (a == 0)) pow = 1.0;
/* This if() is partially incorrect. 0^0 != 0, but it's a special case.*/
else if (a == 0) pow = 0.0;
else if (b > 0)
{
for(i = 1; i <= b; i++)
pow *= a;
}
/* The comment on the else is wrong. b may == 0 here - if it does, this will stack fault. */
else /* b < 0 */
pow = 1.0 / power(a, - b);
return pow;
}```

5. Originally Posted by Cactus_Hugger
First, format this so that me/you/we know what we're looking at. That, and get your original in better shape before going for a recursive.
Code:
```/*Decide between tabs/spaces, and stick to it.*/
double power(double a, int b)
{
double pow = 1.0;
int i;

/* This if() has no effect, pow is already 1.0...*/
if((b == 0) && (a == 0)) pow = 1.0;
/* This if() is partially incorrect. 0^0 != 0, but it's a special case.*/
else if (a == 0) pow = 0.0;
else if (b > 0)
{
for(i = 1; i <= b; i++)
pow *= a;
}
/* The comment on the else is wrong. b may == 0 here - if it does, this will stack fault. */
else /* b < 0 */
pow = 1.0 / power(a, - b);
return pow;
}```

Technically

Code:
```else if (b > 0)
{```
should be

Code:
`else if (b > 0) {`
This subtle point in style only matters if you are doing coding for a major corporation. But whatever. I would have written the function like:

Code:
```double power( double x, int n )
{
if ( n > 0 )
return x * power( x, n- 1 );
else if ( n < 0 )
return ( 1/x ) * power( x, n + 1 );
else
return 1;
}```

6. Excellent feedback -- and so fast! I am impressed; Also highly impressed about the depth of knowledge and help!

7. Originally Posted by Overworked_PhD
Technically

Code:
```else if (b > 0)
{```
should be

Code:
`else if (b > 0) {`
This subtle point in style only matters if you are doing coding for a major corporation.
What in the world are you talking about?

8. Originally Posted by Overworked_PhD
Technically
Code:
```else if (b > 0)
{```
should be
Code:
`else if (b > 0) {`
This subtle point in style only matters if you are doing coding for a major corporation.
(To clarify) Technically, both are correct. The compiler won't care two bits which is used. The only thing to care is a human. Religious wars have been started over which brace style is the best. Looking through some of the code I have, it seems that some of the SDL sub libraries are written using yours. It also appears that libpng and wxWindows are written in mine. (In the small samples of code that I checked, at least.) Neither is particularly predominant, and is the choice of the project developers/leaders. I perfer the former as it aligns the braces, letting me easily match braces, and spaces things out vertically a bit more. See One True Brace Style on wikipedia.

9. Originally Posted by Cactus_Hugger
(To clarify) Technically, both are correct. The compiler won't care two bits which is used. The only thing to care is a human. Religious wars have been started over which brace style is the best. Looking through some of the code I have, it seems that some of the SDL sub libraries are written using yours. It also appears that libpng and wxWindows are written in mine. (In the small samples of code that I checked, at least.) Neither is particularly predominant, and is the choice of the project developers/leaders. I perfer the former as it aligns the braces, letting me easily match braces, and spaces things out vertically a bit more. See One True Brace Style on wikipedia.
Wrong. The style I mentioned is PREDOMINANT in BSD derived code. The other PREDOMINANT form is having the braces indented to mimic LISP. This style appears in GNU software.