Functions
Why use functions?
All the programs we have written so far have been contained within a single
function, main(). In reality, most programs are divided into multiple modules.
There are several reasons for this:
1. Modules facilitate reusability of code. A block of code can be written
once and then executed multiple times throughout a program by way of function
calls. Furthermore, the same block of code can be included in multiple different
programs.
2. Modules hide the details of their implementation from the rest of the
program (called encapsulation). As long as the interface between the module
and the rest of the program doesn't change (i.e. the arguments to the function
and the return data type, if any) then the module itself can be changed or
even totally rewritten without affecting the remainder of the program.
3. The work of writing a program can be split among multiple members of
a workgroup, with each group working on a separate module. The final program
can then be assembled from these building blocks. Each module can be thoroughly
tested for correctness before it is incorporated into the final program.
4. Breaking the program up into bite-sized chunks makes the code easier
to read and the intent of the programmer easier to follow.
3 main steps when using functions:
Function must be declared (function prototype).
Function must be defined (code written).
Function must be called.
Function Prototypes
Functions must be declared before they can be called. The declaration statement
is referred to as a function prototype and has the following form:
functionType functionName (formal parameter list);
some examples:
void SomeFunction( int num, float avg );
char AnotherFunction( int num1, int num2 );
Note:
The function prototype gives the programmer-chosen name for the function,
just as a variable declaration specifies the chosen identifier for the variable.
The return datatype precedes the function name. A void function is one that
returns no data.
The arguments to the function, the datatypes that are passed into the function
when it is called, are enclosed within parentheses. (Identifier names: num,
avg, num1, and num2, are optional in the prototype.)
The function prototype concludes with a semicolon.
Function prototypes can be positioned anywhere prior to the first time the
function is called. Most commonly, the function prototypes are placed either
at the top of the program, after the preprocessor directives, or in a separate
header file that is then #included in the .cpp file.
Function Definitions
The actual code that constitutes the function header and the body of the
function is referred to as the function definition. A function definition
has the following form:
functionType functionName (formal parameter list)
{
statements
}
some examples:
float SomeFunction( int num1, int num2) //The
function header - note no semicolon!
{
variable declarations
//The function body
various C++ statements;
return ( some_float_variable );
}
void AnotherFunction()
{
statements;
return;
}
Note:
If the function returns a value, a return statement must be included.
If the function is a void function, the return statement can be included
(return;) or implied (no return statement).
When the function is defined, the arguments must be given identifier names,
known as formal parameters. These identifiers can be used throughout the body
of the function. Obviously, variables declared within the body of the function
should not be given the same names as the formal parameters.
Function Calls
A function is called, or invoked, from either the main program (sometimes
called the driver) or from another function. The syntax for this call can
take several forms:
to call a void function (one that does not return any data) you simply call
the function as a statement.
functionName (actual parameter list);
to call a function with a return type other than void, you include the function
in an expression on the right of an assignment statement, or you can embed
the call in a cout statement.
variable = functionName(actual parameter list);
or
cout << expression << functionName(actual parameter list);
some examples:
result = CalculateAverage( total, num_of_values ); // assume
variables total and num_of_values were declared prior to calling
the function