3.2
A custom-tailored suit always fits better than one off the rack.My uncle, the tailorThe previous section told you how to use predefined functions. This section tells you
how to define your own functions.
■ DEFINING FUNCTIONS THAT RETURN A VALUE
You can define your own functions, either in the same file as the
main part of your pro-
gram or in a separate file so that the functions can be used by several different programs.
104 Function Basics
The definition is the same in either case, but for now we will assume that the function
definition will be in the same file as the
main part of your program. This subsection dis-
cusses only functions that return a value. A later subsection tells you how to define
void functions.
Display 3.5 contains a sample function definition in a complete program that demon-
strates a call to the function. The function is called
totalCost and takes two arguments—
the price for one item and the number of items for a purchase. The function returns
the total cost, including sales tax, for that many items at the specified price. The func-
tion is called in the same way a predefined function is called. The definition of the
function, which the programmer must write, is a bit more complicated.
The description of the function is given in two parts. The first part is called the
function declaration or function prototype. The following is the function declara-function declaration or tion (function prototype) for the function defined in Display 3.5:
prototypedouble totalCost(int numberParameter, double priceParameter);The first word in a function declaration specifies the type of the value returned by the
type forvalue returnedfunction. Thus, for the function
totalCost, the type of the value returned is
double.
Next, the function declaration tells you the name of the function; in this case,
total-Cost. The function declaration tells you (and the compiler) everything you need to
know in order to write and use a call to the function. It tells you how many arguments
the function needs and what type the arguments should be; in this case, the function
totalCost takes two arguments, the first one of type
int and the second one of type
double. The identifiers
numberParameter and
priceParameter are called formal param-
eters, or parameters for short. A formal parameter is used as a kind of blank, or place-formal parameterholder, to stand in for the argument. When you write a function declaration, you do
not know what the arguments will be, so you use the formal parameters in place of the
arguments. Names of formal parameters can be any valid identifiers. Notice that a
function declaration ends with a semicolon.
Although the function declaration tells you all you need to know to write a function
call, it does not tell you what value will be returned. The value returned is determined
by the function definition. In Display 3.3 the function definition is in lines 24 to 30 of
definitionthe program. A function definition describes how the function computes the value it
returns. A function definition consists of a function header followed by a function body .
The
function header is written similar to the function declaration, except that theheaderheader does not have a semicolon at the end. The value returned is determined by the
statements in the function body.
The function body follows the function header and completes the function defini-
function bodytion. The function body consists of declarations and executable statements enclosed
within a pair of braces. Thus, the function body is just like the body of the
main part of
a program. When the function is called, the argument values are plugged in for the for-
mal parameters, and then the statements in the body are executed. The value returned
by the function is determined when the function executes a
return statement. (Thedetails of this “plugging in” will be discussed in Chapter 4.)
Programmer-Defined Functions 105
Display 3.5 A Function Using a Random Number Generator (part 1 of 2)1 #include <iostream>2 using namespace std;3 double totalCost(int numberParameter, double priceParameter);
Bạn đang xem 3. - ABSOLUTE C PHẦN 3 PPS