Reputation: 91
Learning C here, and I'm pretty confused on how to use function prototypes.
I'm mainly having a problem calling the function into main. I'm sure I have something messed up in here because all this does is print whatever's in memory.
Thank you for the help.
#include <stdio.h>
double source_volt(double vs);
int main()
{
double source_volt(double vs);
double vs;
printf("%lf", vs);
return 0;
}
double source_volt(double vs)
{
int valid = 0;
do
{
printf("Enter source voltage Vs in volts: ");
scanf("%lf", &vs);
if (vs < 0 || vs > 100)
{
valid = 0;
printf("Please enter a number between 0 and 100.\n");
}
else
valid = 1;
}while(!valid);
return vs;
}
Upvotes: 1
Views: 179
Reputation: 9412
The line inside main double source_volt(double vs);
actually redeclares the function. And then vs
in main is being used without initializing it.
The function source_volt
as it's defined would be called like so:
double vs;
vs = source_volt( vs );
However, I'd also like to point out that you're not gaining anything by passing a double into tho function. You could declare a local variable in source_volt
and return it to get the same results.
Upvotes: 2
Reputation: 2372
This is what is not working for you:
int main()
{
double source_volt(double vs); //You already declared the function,
//you dont need the doubles
double vs;
printf("%lf", vs);
return 0;
}
Instead:
int main()
{
double vs;
vs = double source_volt(vs); //The double keyword is gone
printf("%lf", vs);
return 0;
}
But really, you don't need an argument at all in source volt.
You could rewrite that function to:
double source_volt(void)
{
double vs;
int valid = 0;
do
{
printf("Enter source voltage Vs in volts: ");
scanf("%lf", &vs);
if (vs < 0 || vs > 100)
{
valid = 0;
printf("Please enter a number between 0 and 100.\n");
}
else
valid = 1;
}while(!valid);
return vs;
}
Upvotes: 3