double f(x) {
return(4.0/(1 + x*x));
}
This compiles because (a) C still permits some old features that are considered obsolescent, and (b) you're not asking your compiler to enforce the current version of the language.
In modern C, a function should be declared/defined with a prototype, a declaration that specifies the types of any parameters:
double f(int x) {
/* ... */
}
Pre-ANSI C (prior to 1989) didn't support prototypes, and function parameters were defined with a different syntax. For example, you could write:
double f(x)
int x;
{
/* ... */
}
The type of the parameter was not visible to callers, so calling f
with an argument of a type other than int
could have unpredictable results. (This was obviously a problem, which is why prototypes were introduced.) Furthermore, you could omit the int x;
line and the type would default to int
. And you could omit the double
return type and that would also default to int
.
Old-style declarations are still permitted, but it's rarely a good idea to use them. The implicit int
rule was dropped in the 1999 standard.
You should find out how to ask your compiler to enforce the rules of modern C. If you're using gcc, then gcc -std=c11 -pedantic
is a good start.