Here is an example for the instruction function:
void instructions( )
{
cout << "This program will compute the
total cost of purchases made by a user \n";
cout << "The program will apply a 5%
tax to the total cost of items minus any discount. \n";
cout << "The input to this program is
the cost of each item, quantity, and the discount. \n";
cout << "The output is the grand total
cost of all items together. \n";
}
Note that since the function is of type void, no return statement is used. However, a void function may have a return statement, but it will return nothing, i.e.,
return;
may be used in a void function as well. The inclusion of return; in a void function may be inevitable when one wants to exit the function upon reaching a specific condition. For example:
if( cost_per_item <=0)
return;
else
do something .....
Exercise 6.1
Another example of a void function is a function that takes a real
value and displays it with a specific number of decimal points.
void display_it(double x, int precision)
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(precision);
cout << x << endl;
}
Modify the above function so that it displays two real values with a specific precision. Insert your new function in a file called ex61.txt