Function Declarations, Function Calls, and
Function Implementations
- Function Declaration (Prototype):
return_data_type function_name( parameter_list);
- The return_data_type may be any built-in data type or
user defined data type. A void return_data_type
indicates that the function will not return any values. If a
return_data_type is omitted, it implies an int
will be returned by the function
- The function_name can be any valid
(non-keyword) identifier. main is a special
function that identifies the beginning of the program
- The parameter_list is a comma separated list of data
types that will be provided to the function when it is called
- Variable names need not be specified in the
parameter_list, only their data types are required
- Function Calls:
function_name(actual_parameter_list);
- The return_data_type is not specified in a function
call
- The actual_parameter_list are the comma seperated
values of the variables to be used
as arguments by the function. This is also called the call by
value
method of invoking a function.
- After the execution of the function, the returned value replaces the
function call, when it is used in an expression
- The order of the data types in the
actual_parameter_list should be consistent with the order
specified
in the function declaration
- Function Implementation:
return_data_type function_name(formal_parameter_list)
{//The body of the function goes here...}
- The function implementation begins with a function
header
- The function header specifies the return_data_type,
function_name,
formal_parameter_list. The function header is not
terminated
with a semicolon
- The formal_parameter_list is a comma separated list
of
variables (and their data types). These variables have local scope in the
function
- The actual parameters from the function call get 'mapped' onto their
corresponding formal parameters
- The function body must be terminated by a return statement,
which
returns a data value of the same type as that specified in the function
declaration and the function header
- Only one value may be returned
- Example ...
Back to Previous Page
Document:
Local Date:
Last Modified On: