A declaration of a function specifies:
The general form is:
<type> <Name>(<type of arg 1>, … <type of arg N> ) ;
double sqrt(double) ;
Note that an identifier can be added to the declaration but its presence is optional. We can write:
double sqrt(double x);
if we want to, but the "x" is not required and will be ignored by the compiler.
Functions can have a variable number of arguments. The function "printf" is an example of a function that takes several arguments. We declare those functions like this:
int printf(char *, ...);
The ellipsis means "some more arguments".
Why function declarations are important?
When I started programming in C, prototypes for functions didn’t exist. So you could define a function like this:
and in another module write:
fn(7,9);
without any problems.
Well, without any problems at compile time of course. The program crashed or returned nonsense results. When you had a big system of many modules written by several people, the probability that an error like this existed in the program was almost 100%. It is impossible to avoid mistakes like this. You can avoid them most of the time, but it is impossible to avoid them always.
Function prototypes introduced compile time checking of all function calls. There wasn’t anymore this dreaded problem that took us so many debugging hours with the primitive debuggers of that time. In the C++ language, the compiler will abort compilation if a function is used without prototypes. I have thought many times to introduce that into lcc-win32, because ignoring the function prototype is always an error. But, for compatibility reasons I haven’t done it yet. [35]
[34] The interface for using functions with a variable number of arguments is described in the standard header file "stdarg.h". See too functions with variable number of arguments
[35] There is a strong commitment, from the part of the compiler writers, to maintain the code that was written in the language, and to avoid destroying programs that are working. When the standards committee proposed the prototypes, all C code wasn’t using them yet, so a transition period was set up. Compilers would accept the old declarations without prototypes and just emit a warning. Some people say that this period should be over by now (it is more than 10 years that we have prototypes already), but still, new compilers like lcc-win32 are supporting old style declarations.