Can someone help me edit this program. I am a begginer in C and suck at it somewhat. I can't figure out a way to make the function work since i have to initialize s to equal u and if i initialize it in the function it will always make s=u. However if i initialize it in the program s still equals u. I'm lost can someone help me. Here is my code

# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"


int power(int num){
int u,v,r,s;
s=s*v;
}


main()
{
int u,v,r,s,pow;
printf("whats the base");
u=GetInteger();
printf("whats the power");
v=GetInteger();
r=v;
while(r!=0)
{
pow=power(s);
r=r-1;
}
if(u==0&&v<0)
printf("undefined");
else
printf("the result is %d",s);
getchar();
}

Recommended Answers

All 10 Replies

If you declare the function as int then you must return an int value in the function definition.
By the way, I'm not sure if you are aware but there is a built in power function in the math library.
See this for more info
http://www.thinkage.ca/english/gcos/expl/c/lib/pow.html

I tried that but it still dosent work.
Here is my new code
# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"


int power(int num){
int u,v,r,s;
s=s*v;
return(s);
}


main()
{
int u,v,r,s,pow;
printf("whats the base");
u=GetInteger();
printf("whats the power");
v=GetInteger();
r=v;
s=u;
while(r!=0)
{
pow=power(s);
r=r-1;
}
if(u==0&&v<0)
printf("undefined");
else
printf("the result is %d",s);
getchar();
}

As the built in function is declared, the power function should contain 2 parameters, the first being the base and the second being the exponent. Try writing the function with a declaration like this:

double power(double x, int n)

i put in the line but it still dosent work. Here is my new code
# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"
double power(double x, int num)

double power(double x, int num){
int u,v,r,s;
s=s*v;
return(s);
}


main()
{
int u,v,r,s,pow;
printf("whats the base");
u=GetInteger();
printf("whats the power");
v=GetInteger();
r=v;

while(r!=0)
{
pow=power(s);
r=r-1;
}
if(u==0&&v<0)
printf("undefined");
else
printf("the result is %d",s);
getchar();
}

If you have two parameters in your function definition then you need to have two parameters in the function call. Make sure the parameters in the function call are of the same type data type as those in the definitiion.

Why are there unused local variables in the function power?

Also, bear in mind that your function is designed to take two arguments, yet you only supplied one in the function call.

how do i supply a second one?

When you call the function in main(), the two parameters should be the base and the exponent. In you last post these would refer to u and v.

power(u,v);

I tried that yet it still dosent work. it always says "the result is 45" no matter what number i enter. here is what i have now.

# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"


double power(double x, int num){
int u,v,r,s;
s=s*v;
return(s);
}


main()
{
int u,v,r,s,pow;
printf("whats the base");
u=GetInteger();
printf("whats the power");
v=GetInteger();
r=v;

while(r!=0)
{
pow=power(u,v);
r=r-1;
}
if(u==0&&v<0)
printf("undefined");
else
printf("the result is %d",s);
getchar();
}

Try changing int x to double x and change line 9 to:

double s = x * num;

When you declare power as double, that means the return value must be also be double.

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.