In C, in contrast to C++, and assuming you are not using a strict C99 conformance mode in your compiler, then when the compiler comes across something that looks like a function call but the identifier has not previously been declared, then the compiler assumes that it is the name of a function returning an integer.
int main(void)
{
something_functional(1, 2);
}
Without a prototype, the compiler in C90 or relaxed C99 mode will assume that 'something_functional()' is indeed a function returning an integer.
If you subsequently write:
something_functional(double d, char *s)
{
...
}
The compiler will assume that the return type of 'something_functional()' is an int
and will let the code compile (despite the horrendous mismatch between the actual argument types in the call and in the function definition).
If you subsequently write:
void something_functional(douebl d, char *s)
{
...
}
the compiler will, quite correctly, complain that you've told it different things about the function and that makes it erroneous.
There are several ways to fix the problem:
- Move the function definition ahead of the function call. This is what Pascal required, and leads to the main() function appearing at the bottom of the file.
Place a prototype for the function ahead of its use in the file:
static void something_functional(double d, char *s);
I take the view that if the function is not going to be called from outside the source file, it should be static.
Note that C99 in a strict conforming mode does not allow you to declare functions implicitly. C++ never has either.
There's another interesting feature of pre-C99. You could also write:
static c;
static function();
some_function(a, b)
{
}
This defines an integer variable c
, a function called function()
that returns an int
, and a function called some_function()
that takes two int
arguments and returns an int
.
This is not allowed in strict C99 code - and was not particularly good style in C90 code, but was allowed for backwards compatibility with pre-standard C.