5 //number customers. If number is 0, only an error message is output.6 int main( )7 {8 int number;9 double totalWeight;10 cout << "Enter the number of customers: ";11 cin >> number;12 cout << "Enter weight of ice cream to divide (in ounces): ";13 cin >> totalWeight;14 iceCreamDivision(number, totalWeight);15 return 0;16 }17 void iceCreamDivision(int number, double totalWeight)18 {19 double portion;20 if (number == 0)21 {22 cout << "Cannot divide among zero customers.\n";23 return;If number is 0, then thefunction execution ends here.24 } 25 portion = totalWeight/number;26 cout << "Each one receives " 27 << portion << " ounces of ice cream." << endl;28 }SAMPLE DIALOGUEEnter the number of customers: 0Enter weight of ice cream to divide (in ounces): 12Cannot divide among zero customers.
Programmer-Defined Functions 115
returned by the function. For a function that changes the value of some argument vari-
ables, the postcondition will describe all the changes made to the values of the arguments.
For example, the following is a function declaration with precondition and post-
condition:
void showInterest(double balance, double rate);//Precondition: balance is a nonnegative savings account balance. //rate is the interest rate expressed as a percentage, such as 5 for 5%.//Postcondition: The amount of interest on the given balance //at the given rate is shown on the screen.You do not need to know the definition of the function
showInterest in order to use
this function. All that you need to know in order to use this function is given by the
precondition and postcondition.
When the only postcondition is a description of the value returned, programmers
usually omit the word
Postcondition, as in the following example:
double celsius(double fahrenheit);//Precondition: fahrenheit is a temperature in degrees Fahrenheit.//Returns the equivalent temperature expressed in degrees Celsius.Some programmers choose not to use the words precondition and postcondition in
their function comments. However, whether you use the words or not, you should
always think in terms of precondition and postcondition when designing a function
and when deciding what to include in the function comment.
■
main IS A FUNCTION
As we already noted, the
main part of a program is actually the definition of a function
called
main. When the program is run, the function
main is automatically called; it, in
turn, may call other functions. Although it may seem that the
return statement in the
main part of a program should be optional, practically speaking it is not. The C++ stan-
dard says that you can omit the
return 0 statement in the
main part of the program,
but many compilers still require it and almost all compilers allow you to include it. For
the sake of portability, you should include
return 0 statement in the
main function.
You should consider the
main part of a program to be a function that returns a value of
type
int and thus requires a
return statement. Treating the
main part of your program
as a function that returns an integer may sound strange, but that’s the tradition which
many compilers enforce.
Although some compilers may allow you to get away with it, you should not include
a call to
main in your code. Only the system should call
main, which it does when you
run your program.
116 Function Basics
■ RECURSIVE FUNCTIONS
C++ does allow you to define recursive functions. Recursive functions are covered in
Chapter 13. If you do not know what recursive functions are, there is no need to be
concerned until you reach that chapter. If you want to read about recursive functions
early, you can read Sections 13.1 and 13.2 of Chapter 13 after you complete Chapter 4.
Note that the
main function should not be called recursively.
Self-Test Exercises
Bạn đang xem 5 / - ABSOLUTE C PHẦN 3 PPS