A function (DEF) is a named unit which performs some specific operation. Without being aware of functions, you have been using them; main() is a function. In general, functions are indicated by the '()' immediately following the function name. For example, erase() would be a function call to function "erase".
Functions are an effective way to improve your program with respect to clarity for the reader as well as ease of implementation for you, the programmer.
First consider the general look of a function with the different parts numbered (this may look a bit familiar). Syntax elements are found in bold.
(1) ReturnType (2) FunctionName(
(3) arguments passed )
{
(4)
// declarations and initializations
(5) // body
(6)
return
ReturnExpression (no expression if void return type);
}
(1) The return type is the data type
of the information which will be returned from the function.
The return type can also be void
which indicates that no value is returned. If the function is void
there is no return value (return;).
(2) The function name should be descriptive of the operation which the function will perform. The function name is similar to variable names since it should be descriptive as well as simple.
(3) The arguments passed are the variables which you will use in your function which are declared and assigned a value in the calling function. If an argument is in this list, make sure it is used in the function. It is a contradiction of modularity if arguments which are passed are not used within the function; you will also receive a warning from the compiler indicating you have not used an argument. More on this part of functions later since it often causes confusion.
(4) Declare and initialize local variables in the function.
(5) The body of the function is the list of statements which will be executed upon the call to the function.
(6) The return expression is an expression
(e.g. variable, expression, or constant) which must evaluate to the same
data type as the return type. This expression will be returned to
the function call.
int main()
{
int
i;
for
(i = 0; i < 5; i++)
cout << "i is " << i << endl;
return
0;
}
This is a main function; we have seen many examples of this before. Look at the general function given above and compare it to this main function. Consider the parts of the function composed with the general function numbering scheme.
(1) int (2) main(
(3) )
{
(4)
int i;
(5)
for (i = 0; i < 5; i++)
cout << "i is " << i << endl;
(6)
return 0;
}
Note that there are no arguments passed
to this function.
Again, consider the general function skeleton:
(1) ReturnType (2) FunctionName(
(3) arguments passed )
{
(4)
// declarations and initializations
(5) // body
(6)
return
ReturnExpression (no expression if void return type);
}
Consider the following questions:
Does my objective require a specific action which may be repeated or called in this or other functions?
Is there an action which requires a lot of code, that brings a cluttered sense to your program? Would separating the code from the main function provide more clarity?
If your answer is yes to any of the
previous questions, then you probably want to write your own function.
1) Will your function return a value?
a) What data type
will it return?
2) What is the objective of the function?
a) Give it an appropriate
function name indicative of the code it contains.
3) Decide if the function requires that you pass it information. Make sure to include the data type as well as the variable name for each argument:
Ex 1. int main(int argc, char *argv[]);
Ex 2. int swap(int A, int B).
4, 5) Write the code: declarations and body.
6) Return the appropriate expression.
Make sure it matches the type you are expecting in main() and in the ReturnValue
field.
Writing your own functions: examples
(check here for many examples before you proceed).
#include <iostream.h>
int main()
{
int x, y, max;
cin >> x >> y;
max = maximum(x,
y); // the function call; the function returns a value
cout << "The
maximum of the integers you entered was " << max << endl;
return 0;
}
Consider the call to the Maximum function: max = maximum(x, y);
To call the function we indicate the function called and pass the appropriate information as specified in the function definition above. The function expects two integers, therefore we pass it two integers: x and y.
Finally, since maximum() returns an integer, we must make sure we use the return value. In this case, we assign it to an integer variable. We simply could have called the function within the cout statement:
cout << "The maximum of the integers you entered was " << maximum(x, y) << endl;
Remember that the data types are not
specified anywhere within the function call.
void Z() {
cout << "Hello."
<< endl;
return;
}
We will call the function with the function call
Z();
Another example. Using the printError() function defined previously, we call it as follows:
printError();
Functions may have more than one return statement. Although, during execution, once any return statement is reached, the function is exited immediately.
Void functions do not require a return statement. For clarity, it is a good idea to include one at the end of the function.