32.5 degrees Fahrenheit is equivalent to0.3 degrees Celsius.
112 Function Basics
Notice that the function call ends with a semicolon, which tells the compiler that the
function call is an executable statement.
When a
void function is called, the arguments are substituted for the formal param-
eters, and the statements in the function body are executed. For example, a call to the
void function
showResults, which we gave earlier in this section, will cause some out-
put to be written to the screen. One way to think of a call to a
void function is to imag-
ine that the body of the function definition is copied into the program in place of the
function call. When the function is called, the arguments are substituted for the formal
parameters, and then it is just as if the body of the function were lines in the program.
(Chapter 4 describes the process of substituting arguments for formal parameters in
detail. Until then, we will use only simple examples that should be clear enough with-
out a formal description of the substitution process.)
It is perfectly legal, and sometimes useful, to have a function with no arguments. In
functions withno argumentsthat case there simply are no formal parameters listed in the function declaration and
no arguments are used when the function is called. For example, the
void function
initializeScreen, defined below, simply sends a newline command to the screen:
void initializeScreen( ){ cout << endl;}If your program includes the following call to this function as its first executable state-
ment, then the output from the previously run program will be separated from the out-
put for your program:
initializeScreen( ); Be sure to notice that even when there are no parameters to a function, you still must
include the parentheses in the function declaration and in a call to the function.
Placement of the function declaration (function prototype) and the function defini-
tion is the same for
void functions as what we described for functions that return a
value.
■
return STATEMENTS IN
void FUNCTIONS
Both
void functions and functions that return a value can have
return statements. In
voidfunctions and the case of a function that returns a value, the
return statement specifies the value
returnreturned. In the case of a
void function, the
return statement does not include any
statementsexpression for a value returned. A
return statement in a
void function simply ends the
function call. Every function that returns a value must end by executing a
return state-
ment. However, a
void function need not contain a
return statement. If it does not
contain a
return statement, it will end after executing the code in the function body. It
is as if there were an implicit
return statement just before the final closing brace,
}, at
the end of the function body.
Programmer-Defined Functions 113
F
UNCTION D
ECLARATION (F
UNCTION P
ROTOTYPE)
A function declaration (function prototype) tells you all you need to know to write a call to the function. A function declaration (or the full function definition) must appear in your code prior to a call to the function. Function declarations are normally placed before the main part of your program.S
YNTAXDo not forgetthis semicolon.Type_Returned_Or_void FunctionName(Parameter_List);where the Parameter_List is a comma-separated list of parameters:Type_1 Formal_Parameter_1, Type_2 Formal_Parameter_2,... ..., Type_Last Formal_Parameter_LastE
XAMPLESdouble totalWeight(int number, double weightOfOne);//Returns the total weight of number items that//each weigh weightOfOne.void showResults(double fDegrees, double cDegrees);//Displays a message saying fDegrees Fahrenheit//is equivalent to cDegrees Celsius.The fact that there is an implicit
return statement before the final closing brace in a
function body does not mean that you never need a
return statement in a
void func-
tion. For example, the function definition in Display 3.7 might be used as part of a res-
taurant-management program. That function outputs instructions for dividing a given
amount of ice cream among the people at a table. If there are no people at the table
(that is, if
number equals
0), then the
return statement within the
if statement termi-
nates the function call and avoids a division by zero. If
number is not
0, then the func-
tion call ends when the last
cout statement is executed at the end of the function body.
■ PRECONDITIONS AND POSTCONDITIONS
One good way to write a function declaration comment is to break it down into two
kinds of information called the precondition and the postcondition. The precondition
preconditionstates what is assumed to be true when the function is called. The function should not be
used and cannot be expected to perform correctly unless the precondition holds. The
postcondition describes the effect of the function call; that is, the postcondition tellspost-what will be true after the function is executed in a situation in which the precondition
conditionholds. For a function that returns a value, the postcondition will describe the value
114 Function Basics
Display 3.7 Use of return in a void Function1 #include <iostream>2 using namespace std;3 void iceCreamDivision(int number, double totalWeight);
Bạn đang xem 32. - ABSOLUTE C PHẦN 3 PPS