Function Definitions
The general form of a function definition is:
[storage_class] [type]name( [parameter_list] ) // function declarator { /*declarations,statements*/ // function body }
-
storage_class One of the storage class specifiers
externorstatic. Becauseexternis the default storage class for functions, most function definitions do not include a storage class specifier.-
type The type of the function’s return value. This can be either
voidor any other type, except an array.-
name The name of the function.
-
parameter_list The declarations of the function’s parameters. If the function has no parameters, the list is empty.
Here is one example of a function definition:
long sum( int arr[], int len )// Find the sum of the first
{ // len elements of the array arr
int i;
long result = 0;
for( i = 0; i < len; ++i )
result += (long)arr[i];
return result;
}Because by default function names are external names, the functions of a program can be distributed among different source files, and can appear in any sequence within a source file.
Functions that are declared as static, however,
can only be called in the same translation unit in which they are
defined. But it is not possible to define functions with block
scope—in other words, a function definition cannot appear
within another function.
The parameters of a function are ordinary variables whose scope is limited to the function. When the function is called, they are initialized with the values of the arguments received from ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access