Definition of Functions:
A
function definition, also known as function implementation shall include the
following elements;
1.
Function name.
2.
Function type.
3.
List of parameters.
4.
Local variable declarations.
5.
Function statements and
6.
a return statement
All
six elements are grouped into two parts, namely,
- function header (First three elements)
- function body (Second three elements)
Function
Header:
The function header consists of three parts: function return type,
the function name, and the formal parameter list. Note that a semicolon is not
used at the end of the function header.
Name and
Type:
The function return type specify the type of value (like
float, void, int) that the function is expected to return to the program
calling the function. If the return type is not explicitly specified, C will
assume that it is an integer type.
The function
name is any valid C identifier and therefore must follow the same rules of
formation as other variable names in C. The name should be appropriate to the
task performed by the function.
Formal
Parameter List:
The parameter list declares the variable that wil receive the
data sent by the calling program. They serve as input data to the function to
carry out the specified task. Since they represent actual input values, they
are often reffered to as formal parameters. These parameters can also be used
to send values to the calling programs.
The parameter list contains declaration of variables
seperated by commas and surrounded by parentheses. Examples:
float quadratic (int a, int b, int
c) {…..}
int sum (int a, int b) {…..}
Remember, there is no semicolon after the closing
parentheses.
Function
Body:
The function body contains the declarations and statements necessary
for performing the required task. The body enclosed in braces, contains three
parts, in the order given below:
1.
Local declarations that specify the variables needed by the
function.
2.
Function statements that perform the task of the function.
3.
A return statement that returns the value evaluated by the
function.
Post a Comment