A function can be called without declaring it if the function's
return value is int
(although this practice is not
recommended due to the loss of type-checking capability; all
functions should be declared). If the return value is anything else,
and if the function definition is located after the calling function
in the source code, the function must be declared before calling it.
For example:
char lower(int c); /* Function declaration */ caller() /* Calling function */ { int c; char c_out; . . . c_out = lower(c); /* Function call */ } char lower(int c_up) /* Function definition */ { . . . }
If the function definition for lower
was located
before the function caller
in the source code,
lower
would not have to be declared again before
calling it. In that case, the function definition would serve as
its own declaration and would be in scope for any function calls
from within all subsequently defined functions in the same source
file.
Note that both the function definition and function declaration for
lower
are in the prototype style. Although C supports
the old style of function declaration in which the parameter
types are not specified in the function declarator, it is good
programming practice to use prototype declarations for all user-
defined functions in your program, and to place the prototypes
before the first use of the function. Also note that it is valid for
the parameter identifier in the function declaration to be different
from the parameter identifier in the function definition.
In a function declaration, the void
keyword should be
used to specify an empty argument list. For example:
char function_name(void);
As with function definitions, the void
keyword can also
be used in function declarations to specify the return value type
for functions that do not return a value. For example:
main() { void function_name( ); . . . } void function_name( ) { }