|

Using
functions in your program requires that you first
declare the function and that you then define the
function. The declaration tells the compiler the
name, return type, and parameters of the function.
The definition tells the compiler how the function
works. No function can be called from any other
function that hasn’t first been declared. The declaration
of a function is called its prototype.
Declaring
the Function
There
are three ways to declare a function:
·
Write your prototype into a file, and then use the
#include directive to include it in your program.
·
Write the prototype into the file in which your
function is used.
·
Define the function before it is called by any other
function. When you do this, the definition acts
as its own declaration.
Although
you can define the function before using it, and
thus avoid the necessity of creating a function
prototype, this is not good programming practice
for three reasons.
First,
it is a bad idea to require that functions appear
in a file in a particular order. Doing so makes
it hard to maintain the program as requirements
change.
Second,
it is possible that function A() needs to be able
to call function B(), but function B() also needs
to be able to call function A() under some circumstances.
It is not possible to define function A() before
you define function B() and also to define function
B() before you define function A(), so at least
one of them must be declared in any case.
Third,
function prototypes are a good and powerful debugging
technique. If your prototype declares that your
function takes a particular set of parameters, or
that it returns a particular type of value, and
then your function does not match the prototype,
the compiler can flag your error instead of waiting
for it to show itself when you run the program.
Function
Prototypes
Many
of the built-in functions you use will have their
function prototypes already written in the files
you include in your program by using #include. For
functions you write yourself, you must include the
prototype.
The
function prototype is a statement, which means it
ends with a semicolon. It consists of the function’s
return type, name, and parameter list.
The
parameter list is a list of all the parameters and
their types, separated by commas. Figure 5.2 illustrates
the parts of the function prototype.

Figure 5.2. Parts of a function prototype.
The
function prototype and the function definition must
agree exactly about the return type, the name, and
the parameter list. If they do not agree, you will
get a compile-time error. Note, however, that the
function prototype does not need to contain the
names of the parameters, just their types. A prototype
that looks like this is perfectly legal:
long
Area(int, int);
This
prototype declares a function named Area() that
returns a long and that has two parameters, both
integers. Although this is legal, it is not a good
idea. Adding parameter names makes your prototype
clearer. The same function with named parameters
might be
long
Area(int length, int width);
It
is now obvious what this function does and what
the parameters are.
Note
that all functions have a return type. If none is
explicitly stated, the return type defaults to int.
Your programs will be easier to understand, however,
if you explicitly declare the return type of every
function, including main(). Listing 5.1 demonstrates
a program that includes a function prototype for
the Area() function.
Listing
5.1. A function declaration and the definition
and use of that function.
1:
// Listing 5.1 - demonstrates the use of function
prototypes
2:
3:
typedef unsigned short USHORT;
4:
#include <iostream.h>
5:
USHORT FindArea(USHORT length, USHORT width); //function
prototype
6:
7:
int main()
8:
{
9:
USHORT lengthOfYard;
10:
USHORT widthOfYard;
11:
USHORT areaOfYard;
12:
13:
cout << "\nHow wide is your yard? ";
14:
cin >> widthOfYard;
15:
cout << "\nHow long is your yard? ";
16:
cin >> lengthOfYard;
17:
18:
areaOfYard= FindArea(lengthOfYard,widthOfYard);
19:
20:
cout << "\nYour yard is ";
21:
cout << areaOfYard;
22:
cout << " square feet\n\n";
23:
return 0;
24:
}
25:
26:
USHORT FindArea(USHORT l, USHORT w)
27:
{
28:
return l * w;
29:
}
Output:
How wide is your yard? 100
How long is your yard? 200
Your yard is 20000 square feet
Analysis: The prototype for the FindArea()
function is on line 5. Compare the prototype with
the definition of the function on line 26. Note
that the name, the return type, and the parameter
types are the same. If they were different, a compiler
error would have been generated. In fact, the only
required difference is that the function prototype
ends with a semicolon and has no body.
Also note that the parameter names in the prototype
are length and width, but the parameter names in
the definition are l and w. As discussed, the names
in the prototype are not used; they are there as
information to the programmer. When they are included,
they should match the implementation when possible.
This is a matter of good programming style and reduces
confusion, but it is not required, as you see here.
The
arguments are passed in to the function in the order
in which they are declared and defined, but there
is no matching of the names. Had you passed in widthOfYard,
followed by lengthOfYard, the FindArea() function
would have used the value in widthOfYard for length
and lengthOfYard for width. The body of the function
is always enclosed in braces, even when it consists
of only one statement, as in this case.
Defining
the Function
The
definition of a function consists of the function
header and its body. The header is exactly like
the function prototype, except that the parameters
must be named, and there is no terminating semicolon.
The
body of the function is a set of statements enclosed
in braces. Figure 5.3 shows the header and body
of a function.
Figure 5.3. The header and body of a function.
|