error: a parameter list without types is only allowed in a function definition
Solution 1
Your problem
What you wrote is recognized by your compiler as a function prototype. So it tells you that you should have something like this, with types:
bool tri(double x, double y, double z);
Solution
Here, you don't want a function prototype, instead, you want to call your function. You don't need to specify the return type when calling a function.
So simply do this:
tri(x, y, z);
...instead of bool tri(x, y, z);
.
Solution 2
A couple of small changes are suggested below - most notably you put the return value of the function into a bool
called val
declared at the top and then print out at the end the values of x,y,z
and val
which is the true or false returned by your function.
Note you need to use the function you have written and something that returns a values which is why you need something like val = tri (x, y, z);
to capture the returned value, which you can then print out...
#include <stdio.h>
#include <cs50.h>
#include <stdbool.h>
bool tri (double x, double y, double z);
int main(void)
{
bool val;
double x = get_double("Give:");
double y = get_double("Another:");
double z = get_double("Now:");
val = tri (x, y, z);
printf("x=%g, y=%g, z=%g\n",x,y,z);
printf("%s", val ? "true" : "false");
return 0;
}
bool tri (double x, double y, double z)
{
if (x<1 || y<1 || z<1)
{
return false;
}
if (x+y > z && y+z > x && z+x > y)
{
return true;
}
else
{
return false;
}
}
Related videos on Youtube
callmeanythingyouwant
Updated on June 04, 2022Comments
-
callmeanythingyouwant about 2 years
I'm learning how to create and use functions. My code doesn't compile but I can't seem to figure out what I'm doing wrong.
Here is my code:
#include <stdio.h> #include <cs50.h> #include <stdbool.h> bool tri (double x, double y, double z); int main(void) { double x = get_double("Give:"); double y = get_double("Another:"); double z = get_double("Now:"); bool tri (x, y, z); } bool tri (double x, double y, double z) { if (x<1 || y<1 || z<1) { return false; } if (x+y > z && y+z > x && z+x > y) { return true; } else { return false; } }
When I try to compile this code, I get the following error:
error: a parameter list without types is only allowed in a function definition bool tri (x, y, z); ^
-
callmeanythingyouwant about 6 yearsPerfect. I understand it now. Thank you!
-
callmeanythingyouwant about 6 yearsI realise I don't understand the usage of bool very well. I had no idea I could declare it like you declare an integer. Thank you so much!
-
tom about 6 years@callmeanythingyouwant - no problem, but note you can only use
bool
because you include thestdbool.h
header at the top of your code.