| CMPSC 101 - Functions Worksheet #1 | Name - |
Draw a console window and show the expected output for each code segmeent as precisely as possible.
1.
int add(int a, int b);
int main()
{
int num1 = 10;
int num2 = 15;
int result = 0;
result = add(num1, num2);
cout << result << endl;
return 0;
}// end of main
int add(int a, int b)
{
int sum = a + b;
return sum;
}// end of add
2.
double computeDiscount(int num, double price);
const double CLEARANCE_DISCOUNT = 0.2;
int main()
{
double pricePerWidget = 19.99;
int numWidgets = 4;
double discount = 0.0;
double basePrice = 0.0;
double finalPrice = 0.0;
basePrice = numWidgets * pricePerWidget;
discount = computeDiscount(numWidgets, pricePerWidget);
finalPrice = basePrice - discount;
cout << finalPrice << endl;
return 0;
}// end of main
double computeDiscount(int num, double price)
{
double base = num * price;
return CLEARANCE_DISCOUNT * base;
}// end of computeDiscount