Developing functions in this manner is referred to as procedural abstraction.
Perhaps an example will help to demonstrate top-down design; suppose you want to find the area of a circle with the radius the user provides and output the result.
// Determine the area of a circle from the user input radius and display the // result.
// Determine the area of a circle from the user input radius and display the // result. // Need mechanisms to get input from the user and output the result. // define what PI is in this case.
// Determine the area of a circle from the user input radius and display the // result. // Need mechanisms to get input from the user and output the result. // define what PI is in this case. // 1) Get the radius value from the user.. return of type double double askUser(); // 2) Calculate the area of the circle given a radius input.. also return type double double calculateArea( double radius ); // 3) Output the area, given the area as an argument. void displayArea( double area );
main function to perform these operations in the
correct order, and with correct output:
int main() {
double inputRadius = 0.0, circleArea = 0.0;
// 1) Get the radius value from the user
inputRadius = askUser();
// 2) Calculate the area of the circle with that radius
circleArea = calculateArea( inputRadius );
// 3) Output the area
displayArea( circleArea );
}
// 1) Get the radius value from the user.. return of type double
double askUser() {
double input;
cout << "Please give the radius of the circle: ";
cin >> input;
cout << endl;
return input;
}
// 2) Calculate the area of the circle given a radius input.. also return type double
double calculateArea( double rad ) {
return (PI * radius * radius); // area is PI * (radius squared)
}
// 3) Output the result
double outputResult( double result ) {
cout << "The area is = " << result << endl;
}
// Determine the area of a circle from the user input radius and display the
// result.
// Need mechanisms to get input from the user and output the result.
#include <iostream>;
using namespace std;
// define what PI is in this case.
const PI = 3.1415926535;
// 1) Get the radius value from the user.. return of type double
double askUser() {
double input;
cout << "Please give the radius of the circle: ";
cin >> input;
cout << endl;
return input;
}
// 2) Calculate the area of the circle given a radius input.. also return type double
double calculateArea( double rad ) {
return (PI * radius * radius); // area is PI * (radius squared)
}
// 3) Output the result
double outputResult( double result ) {
cout << "The area is = " << result << endl;
}
int main() {
double inputRadius = 0.0, circleArea = 0.0;
// 1) Get the radius value from the user
inputRadius = askUser();
// 2) Calculate the area of the circle with that radius
circleArea = calculateArea( inputRadius );
// 3) Output the area
displayArea( circleArea );
}
main()
calls askUser() correctly.
From this perspective, the function askUser() is essentially a
black box.
You do not care how it generates its result, so you can implement the function
as a stub---a simplified version of the final function encoding:
double askUser() { // implementing as a stub to return 4.5 for debugging
return 4.5; // an arbritrary value
}
|
double askUser() { // still in stub form and incomplete
double input = 0.0;
input = 4.5;
return input;
}
|
calculateArea(), you
can have main() be a driver---a "dummy" or "test" version
to see if the function does what is expected given an expected value:
int main() { // this driver version is not complete but for debugging only!
double area = calculateArea( 4.5 );
cout << "area for 4.5 is " << area << endl;
}
|
main() function was very
simplified to check whether the function calculateArea(...)
worked correctly.
#include<iostream>
using namespace std;
/* Function declarations */
int average(int first_number, int second_number, int third_number);
int average(int first_number, int second_number);
/* MAIN PROGRAM: */
int main() {
int number_A = 5, number_B = 3, number_C = 10;
cout << "The integer average of " << number_A << " and ";
cout << number_B << " is ";
cout << average(number_A, number_B) << ".\n\n";
cout << "The integer average of " << number_A << ", ";
cout << number_B << " and " << number_C << " is ";
cout << average(number_A, number_B, number_C) << ".\n";
return 0;
}
/* END OF MAIN PROGRAM */
/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 3 INTEGERS: */
int average(int first_number, int second_number, int third_number) {
int sum; //This is a local variable
sum = first_number + second_number + third_number;
return ( sum / 3);
}
/* END OF AVERAGE FUNCTION ONE */
/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 2 INTEGERS: */
int average(int first_number, int second_number) {
int sum; //This is another local variable.
sum = first_number + second_number;
return ( sum / 2);
}
/* END OF AVERAGE FUNCTION TWO */
|
The integer average of 5 and 3 is 4.
The integer average of 5, 3 and 10 is 6.
|
average, are
distingushable because their number of input parameters differ.
Your assignment is to write a program with a set of overloaded functions with
the name areaRect that, given various kinds of input, compute the
area of rectangle.
Put the definitions of all these functions after the main()
function (the declarations must go beforehand, of course).
There are five versions:
| Hint: | You may want to use the fabs() function that returns the
absolute value of a double from the cmath library.
|
|---|
Some final notes:
areaRect functions are black boxes for users, so
make sure you include sufficiently descriptive comments to inform users exactly
what they do (not HOW they do it).
In other words, a user of these functions should be able to read only your
header comments to correctly use each of your functions.
main function should be small (as in the average example above)
that operates as a small interactive driver test program for each of your
areaRect functions.
The user should be able to enter one rectangle of each type.
// Course: CSC 306 Introduction to Programming with C++
// Name: Your Name
// Assignment #7: <Put a brief sentence about your program here.>
/*
Purpose: <Put a more in-depth description of the program here.>
*/
When you are finished writing and testing your assignment, drop your source code, YourLastName_306A7.cpp, into the CSC306_A07 dropbox on the Academic server.