// // This program demonstrates the use of a PROGRAMMER DEFINED // FUNCTION. A function is like a mini-program. // #include <iostream> using namespace std; double total_cost(int number_par, double price_par); // Computes total cost including sales tax on number_par // items at cost of price_par each. int main(void) { double price = 0.0; double bill = 0.0; int number = 0; cout << "Number items: "; cin >> number; cout << "Price per item: "; cin >> price; // Calling a PROGRAMMER DEFINED FUNCTION here!!! // This is exactly like calling a predefined function for // example pow(2.0,3.0) or sqrt(4.0). The only difference is // that YOU have to write the code for the function. bill = total_cost(number,price); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << number << " items at $" << price << " each." << endl << "Final bill including tax $" << bill << endl; return 0; } // Here's the function definition! Think of a function like a // mini-program. The INPUT to the function is provided by the // parameters passed into the function from the caller. The // OUTPUT from the function provided to the caller when the // function executes a RETURN statement. double total_cost(int number_par, double price_par) { const double TAX_RATE = 0.05; double result =0.0; // Compute the subtotal. Notice that I'm using // the parameters passed into the function by the // caller to compute this value. result = number_par * price_par; // Compute the total price. That's the subtotal plus // tax on the subtotal result = result + result*TAX_RATE; // Return the total price to the caller return result; }