# Making a function x^n

This is a discussion on Making a function x^n within the C Programming forums, part of the General Programming Boards category; I'd like to be able to make a function x^n, where x is a float, and n is a positive ...

1. ## Making a function x^n

I'd like to be able to make a function x^n, where x is a float, and n is a positive integer.

What about something like this?:

Code:
```int a;
float x_n=1;
float nthpower(float x, int n)
{
for(a=0;a<=n;a++);
x_n=x_n*x;
}```

2. Except a and x_n need to be defined inside the function, rather than at global scope. You also need to return your answer. And your for-loop goes one too many times.

3. There is already such a function, called pow().
First of all what you do is wrong. you would want:
Code:
```float nthpower(float x, int n)
{
int a;
float x_n=1;
for(a=0;a<n;a++);
x_n=x_n*x;
return x_n;
}```
1) You need int a, float x_n inside the function
2) You ned < not <=
3) You need of course a return;
4) You need to check for n begin negative. If it's 0 it's OK
5) There are might be more optimized ways to do so

4. Code:
```   for(a=0;a<n;a++);
x_n=x_n*x;```
The semicolon above is wrong too.

--
Mats

5. Originally Posted by tabstop
Except a and x_n need to be defined inside the function, rather than at global scope. You also need to return your answer. And your for-loop goes one too many times.
Ok, is this better?

Code:
```float nthpower(float x, int n)
{
int a;
float x_n=1;
for(a=1;a<=n;a++)
x_n=x_n*x;
return x_n;
}```
And I can have an if function in there to see whether n is positive.

6. Originally Posted by bertazoid
Ok, is this better?

Code:
```float nthpower(float x, int n)
{
int a;
float x_n=1;
for(a=1;a<=n;a++);
x_n=x_n*x;
return x_n;
}```
Yeah except the simicolon has to go away!

7. Originally Posted by C_ntua
There is already such a function, called pow().

Ahh ok, so thats part of <math.h> ? I'm enjoying learning how to make such functions anyway

8. Originally Posted by C_ntua
Yeah except the simicolon has to go away!
lol I've never known such a fast forum, it's like instant messaging, by the time I have edited/posted a reply, there is already more replies. Not complaining though, other forums I've been to have been dead. And thanks all for the help.

9. you can solve it by recursion:
Code:
```power (int x,int n) {

if  (n==0){

return 1;
}
else
{
return power(x,n-1)*x;
}
}```

10. i cant understand where is my bug in this code
Code:
```#include <stdio.h>
power (int x,int n);

int main()
{

printf("&#37;d",power(2,1);  //i got a bug in this place i dont know why

return 0;
}
int power (int x,int n) {

if  (n==0){

return 1;
}
else
{
return power(x,n-1)*x;
}
}```

11. Do you mean the missing ) on the printf, or something else? A "bug" in my mind is not a typo that the compiler can detect, but something that causes incorrect behaviour (result, sequence of events, etc) compared to what you expect.

--
Mats

12. Originally Posted by transgalactic2
you can solve it by recursion:
Code:
```power (int x,int n) {

if  (n==0){

return 1;
}
else
{
return power(x,n-1)*x;
}
}```
Thanks

13. oohhh such a foolishness
sorry

my recursion works

Popular pages Recent additions