Below are a set of code examples demonstrating the declaration, definition and use of functions. The first one represents a very simple program that could easily be done without functions. A version of this program written without functions can be found in Chapter 3, Section VI. The code here is very similar in terms of the functional decomposition to that discussed in Chapter 4 of the Story of C++ and written as ch4prg2.cpp
// A program to calculate sale prices based on a single discount percentage and output the
// difference between the original price total and the discounted price total.
// Function version
// File: ch4xmpl1.cpp
#include <iostream.h>
#include <conio.h> // This includes functions to clear the screen;
// such as clrscr();
#include <stdio.h // includes getchar() - used in DisplayIntro
void DisplayIntro();
// Purpose: To display an introductory screen
// Receives: NONE
// Returns: NONE
void DisplayExit();
/ Purpose: To display an exit screen
// Receives: NONE
// Returns: NONE
double GetPrice();
/ Purpose: Get the price from the user
// Receives: NONE
// Returns: <price>, a double
double CalculateNewPrice(double price);
/ Purpose: To calculate the discounted price
// Receives: <price>, a double
// Returns: <newPrice>, a double
void DisplayNewPrice(double newPrice);
/ Purpose: To display the discounted price
// Receives: <newPrice>, a double
// Returns: NONE
void DisplayTotals(double originalPriceTotal, double discountedPriceTotal);
/ Purpose: To display the totals
// Receives: <originalPriceTotal>, a double <discountedPriceTotal>, a double
// Returns: NONE
const double DISCOUNT = .87;
void main()
// Purpose: to calculate sale prices based on a single discount percentage and output the
// difference the original price total and the discounted price total.
// Receives: NONE
// Returns: NONE
{
double price;
double discountedPrice;
double originalPriceTotal = 0;
double discountedPriceTotal = 0;
DisplayIntro();
price = GetPrice();
while (price != -1)
{ discountedPrice = CalculateNewPrice(price);
DisplayNewPrice(discountedPrice);
originalPriceTotal += price;
discountedPriceTotal += discountedPrice;
price = GetPrice();
}
DisplayTotals(originalPriceTotal, discountedPriceTotal);
}
void DisplayExit()
{ cout << "The 13 Percent Discount Program is ending.\n";
}
void DisplayTotals(double originalPriceTotal, double discountedPriceTotal)
{ double totalDiscount;
totalDiscount = originalPriceTotal - discountedPriceTotal;
cout << "The total of the original prices is: $" << originalPriceTotal << endl ;
cout << "The total of the discounted prices is: $" << discountedPriceTotal << endl;
cout << "The total amount discounted is: $" << totalDiscount << endl;
}
double GetPrice()
{ double price;
cout << "Please enter an item’s original price (Enter -1 to Stop) ";
cin >> price;
return price;
}
void DisplayIntro()
{ clrscr();
cout << "You are running the 13 Percent Discount Program\n\n";
cout << "Hit Enter when ready to continue\n";
getchar();
}
double CalculateNewPrice(double price)
{ double newPrice;
newPrice = price * DISCOUNT;
return newPrice;
}
void DisplayNewPrice(double newPrice)
{ cout << "The New Price is: $" << newPrice << endl;
}