In Java, the use of a function may appear earlier than
its definition. In C, all functions being used in a
source file EM(should) be declared somewhere earlier than
their invocations in that file, allowing the compiler to
check if the call's arguments match the function's formal
parameters. A function declaration (or
prototype) looks like a function definition,
but its body (the code between and including the braces
(‘{
’ and ‘}
’)) is replaced by a semicolon
(syntactically similar to a native
method, or an interface method,
in Java). If the compiler finds a function invocation
before any declaration, it will try to infer a
declaration from the invocation, and this may not match
the true definition. A proper declaration can be inferred
from a function definition, should that be encountered
first.
/* a declaration; parameter names may be omitted */
int power(int base, int exponent);
/* From here until the end of the file, we can make calls to power()
,
even though the definition hasn't been encountered. */
/* a definition; parameter names do not need to match declaration */
int power(int b, int e)
{
int r = 1;
while (e-- > 0)
r *= b;
return r;
}