5 //on numberParameter items at a cost of priceParameter each.Function declaration;6 int main( )also called the function 7 {prototype8 double price, bill;9 int number;10 cout << "Enter the number of items purchased: ";11 cin >> number;12 cout << "Enter the price per item $";13 cin >> price;Function call14 bill = totalCost(number, price);15 cout.setf(ios::fixed);16 cout.setf(ios::showpoint);17 cout.precision(2);18 cout << number << " items at "19 << "$" << price << " each.\n"20 << "Final bill, including tax, is $" << bill21 << endl;Function22 return 0;head23 }24 double totalCost(int numberParameter, double priceParameter)25 {26 const double TAXRATE = 0.05; //5% sales tax27 double subtotal;definitionbody28 subtotal = priceParameter * numberParameter;29 return (subtotal + subtotal*TAXRATE);30 }SAMPLE DIALOGUEEnter the number of items purchased: 2Enter the price per item: $10.102 items at $10.10 each.Final bill, including tax, is $21.21
106 Function Basics
A
return statement consists of the keyword return followed by an expression. The
returnfunction definition in Display 3.5 contains the following
return statement:
statementreturn (subtotal + subtotal*TAXRATE);When this
return statement is executed, the value of the following expression is
returned as the value of the function call:
(subtotal + subtotal*TAXRATE)The parentheses are not needed. The program will run the same if the parentheses are
omitted. However, with longer expressions, the parentheses make the
return statement
easier to read. For consistency, some programmers advocate using these parentheses
even with simple expressions. In the function definition in Display 3.3 there are no
statements after the
return statement, but if there were, they would not be executed.
When a
return statement is executed, the function call ends.
Note that the function body can contain any C++ statements and that the state-
ments will be executed when the function is called. Thus, a function that returns a
value may do any other action as well as return a value. In most cases, however, the
main purpose of a function that returns a value is to return that value.
Either the complete function definition or the function declaration (function proto-
type) must appear in the code before the function is called. The most typical arrange-
ment is for the function declaration and the
main part of the program to appear in one
or more files, with the function declaration before the
main part of the program, and
for the function definition to appear in another file. We have not yet discussed dividing
a program across more than one file, and so we will place the function definitions after
the
main part of the program. If the full function definition is placed before the
mainpart of the program, the function declaration can be omitted.
■ ALTERNATE FORM FOR FUNCTION DECLARATIONS
You are not required to list formal parameter names in a function declaration (function
prototype). The following two function declarations are equivalent:
double totalCost(int numberParameter, double priceParameter);and
double totalCost(int, double);We will usually use the first form so that we can refer to the formal parameters in the
comment that accompanies the function declaration. However, you will often see the
second form in manuals.
This alternate form applies only to function declarations. A function definition must
always list the formal parameter names.Programmer-Defined Functions 107
Pitfall
A
RGUMENTSINTHE W
RONG O
RDERWhen a function is called, the computer substitutes the first argument for the first formal param-eter, the second argument for the second formal parameter, and so forth. Although the computer checks the type of each argument, it does not check for reasonableness. If you confuse the order of the arguments, the program will not do what you want it to do. If there is a type violation due to an argument of the wrong type, then you will get an error message. If there is no type viola-tion, your program will probably run normally but produce an incorrect value for the value returned by the function.U
SEOFTHE T
ERMSP
ARAMETER ANDA
RGUMENTThe use of the terms formal parameter and argument that we follow in this book is consistent with common usage, but people also often use the terms parameter and argument interchangeably. When you see the terms parameter and argument,
you must determine their exact meaning from context. Many people use the term parameter for both what we call formalparameters and what we call arguments. Other people use the term argument both for what we call formalparameters and what we call arguments. Do not expect consistency in how people use these two terms. (In this book we sometimes use the term parameter to mean formalparameter, but this is more of an abbreviation than a true inconsistency.)■ FUNCTIONS CALLING FUNCTIONS
A function body may contain a call to another function. The situation for these sorts of
function calls is the same as if the function call had occurred in the
main part of the
program; the only restriction is that the function declaration (or function definition)
must appear before the function is used. If you set up your programs as we have been
doing, this will happen automatically, since all function declarations come before the
main part of the program and all function definitions come after the
main part of the
program. Although you may include a function call within the definition of another
function, you cannot place the definition of one function within the body of another
function definition.
Example
A R
OUNDING F
UNCTIONThe table of predefined functions (Display 3.2) does not include any function for rounding a number. The functions ceil and floor are almost, but not quite, rounding functions. The func-tion ceil always returns the next-highest whole number (or its argument if it happens to be a whole number). So, ceil(2.1) returns 3.0, not 2.0. The function floor always returns the nearest whole number less than (or equal to) the argument. So, floor(2.9) returns 2.0, not 108 Function Basics
Bạn đang xem 5 / - ABSOLUTE C PHẦN 3 PPS