double sqrt (double c);
is a function declaration. (It is also a function prototype). This is how you announce that a function exists, and what parameters it takes and what it returns. The word c
here does not mean anything, it can be omitted.
When you want to call a function you do not repeat this info. You just give the function name, followed by a list of values, e.g. sqrt(c)
.
In your code , sqrt
has not been declared. Functions must be declared before they are called. It's not possible to simultaneously declare a function and call it; you have to declare it first.
(Historical note: this is true from C99 onwards; in C89 you could call an undeclared function, and the compiler would assume you wanted the function declared to return int
; this would cause undefined behaviour for any function that does not actually return int
).
It is possible for you to provide your own declaration, so long as it matches the standard declaration:
double sqrt(double);
However it is a better idea to just use the standard declaration by going:
#include <math.h>
which works because the file math.h
includes the line double sqrt(double);
(or something equivalent).
Then you can write:
result = sqrt(c);
Note that you do not need to use any casts in relation to this function call. Since a function prototype exists, the compiler knows that even if you supply an int
, it should convert that int
to a double
(which it knows how to do), and vice versa.
pt()
to return anint
in the first place? The hypotenuse of a right triangle whose sides are of integer length is usually not itself an integer. Also, you want(b * b)
rather than(b + b)
.