Computing & Information Systems    Department Search CIS Site      Tutorials

## Software Design Using C++

### Review of Functions and Parameters

 `````` /* Filename: function.cpp Author: Br. David Carlson Date: December 21,2003 This is a test program that totals the cost for buying a list of items. The price and number of units to buy for each item are hard-coded in this test program. Tax is not added, but the program does give a discount of \$5 if the cost for all units of any particular item is over \$80. The price, number of units, and costs for each item and the total cost are printed on the screen. */ #include using namespace std; // Function prototypes: void CalculateCost(float Price, int NumPurchased, float & Cost); void Results(float Price, int NumPurchased, float & TotalCost); int main(void) { float Price, TotalCost; int Num; TotalCost = 0.0; Price = 3.89; Num = 2; Results(Price, Num, TotalCost); Price = 30.21; Num = 3; Results(Price, Num, TotalCost); cout << "Total cost: " << TotalCost << endl; return 0; } `````` `````` /* Given: Price The unit price of an item to be purchased. NumPurchased The number of this item to be purchased. TotalCost The total cost of all items purchased thus far. Task: To find the cost of purchasing NumPurchased units of this item and to print this cost on the screen, together with Price and NumPurchased. Also updates TotalCost to include the cost of purchasing these new items. Return: TotalCost Updated total cost of all items purchased thus far. */ void Results(float Price, int NumPurchased, float & TotalCost) { float Cost; CalculateCost(Price, NumPurchased, Cost); TotalCost = TotalCost + Cost; cout << "Unit price: " << Price << " Number purchased: " << NumPurchased << " Cost: " << Cost << endl << endl; } `````` `````` /* Given: Price The unit price of an item to be purchased. NumPurchased The number of this item to be purchased. Task: To find the cost of purchasing NumPurchased items at unit price Price. A discount of \$5 is given if the cost of the NumPurchased items is over \$80. Return: Cost This cost. */ void CalculateCost(float Price, int NumPurchased, float & Cost) { Cost = Price * NumPurchased; if (Cost > 80.0) Cost = Cost - 5.0; } ``````