730 likes | 833 Views
Functions. pointDist1.cpp. // compute the distances between three points ... int main() { double px, py, qx, qy, rx, ry; double dx, dy, dist_pq, dist_pr, dist_qr; // read input cout << "Enter point 1 (2 floats): "; cin >> px >> py; cout << "Enter point 2 (2 floats): ";
E N D
Functions The Ohio State University
pointDist1.cpp // compute the distances between three points ... int main() { double px, py, qx, qy, rx, ry; double dx, dy, dist_pq, dist_pr, dist_qr; // read input cout << "Enter point 1 (2 floats): "; cin >> px >> py; cout << "Enter point 2 (2 floats): "; cin >> qx >> qy; cout << "Enter point 3 (2 floats): "; cin >> rx >> ry; The Ohio State University
pointDist1.cpp (2) ... // calculate distances dx = px - qx; dy = py - qy; dist_pq = sqrt(dx*dx + dy*dy); dx = px - rx; dy = py - ry; dist_pr = sqrt(dx*dx + dy*dy); dx = qx - rx; dy = qy - ry; dist_qr = sqrt(dx*dx + dy*dy); ... The Ohio State University
pointDist1.cpp (3) ... // output distances cout << "Distance between (" << px << "," << py << ") " << "and (" << qx <<"," << qy << ") = " << dist_pq << endl; cout << "Distance between (" << px << "," << py << ") " << "and (" << rx <<"," << ry << ") = " << dist_pr << endl; cout << "Distance between (" << qx << "," << qy << ") " << "and (" << rx <<"," << ry << ") = " << dist_qr << endl; return 0; } The Ohio State University
pointDist1.cpp (3) > pointDist1.exe Enter point 1 (2 floats): 0 0 Enter point 2 (2 floats): 3 0 Enter point 3 (2 floats): 0 4 Distance between (0,0) and (3,0) = 3 Distance between (0,0) and (0,4) = 4 Distance between (3,0) and (0,4) = 5 5 4 3 The Ohio State University
pointDist1.cpp (2) ... // calculate distances dx = px - qx; dy = py - qy; dist_pq = sqrt(dx*dx + dy*dy); dx = px - rx; dy = py - ry; dist_pr = sqrt(dx*dx + dy*dy); dx = qx - rx; dy = qy - ry; dist_qr = sqrt(dx*dx + dy*dy); ... The Ohio State University
Functions Examples of C++ functions: • sqrt(a) • exp(a) • cos(a) • pow(a,b) The Ohio State University
Functions From wikipedia.org: • “A function is a portion of code within a larger program which performs a specific task and is relatively independent of the remaining code.” • “The components of a function include: • A body of code to be executed when the function is called; • Parameters that are passed to the function; • A value that is returned by the function.” The Ohio State University
Function dist() // function dist double dist(double x1, double y1, double x2, double y2) // four input parameters: x1, y1, x2, y2 // function dist returns a value of type double { double dx, dy, d; dx = x1 - x2; dy = y1 - y2; d = sqrt(dx*dx + dy*dy); // return the distance from (x1,y1) to (x2,y2) return(d); } The Ohio State University
A Breakdown of Function dist()(1) double dist(double x1, double y1, double x2, double y2) • This statement is called the function header: • The first doublesays that this function returns a value of type double. This is the return data type. • The function’s name is dist. • This function has 4 parameters: • A parameter of type double called x1 • A parameter of type double called y1 • A parameter of type double called x2 • A parameter of type double called y2 The Ohio State University
A Breakdown of Function dist() (2) double dist(double x1, double y1, double x2, double y2) { double dx, dy, d; dx = x1 - x2; dy = y1 - y2; d = sqrt(dx*dx + dy*dy); // return the distance from (x1,y1) to (x2,y2) return(d); } • Lines after the function header form the body of the function. They calculate and return the distance between points (x1,y1) and (x2,y2). • Notice, because we are returning d which is of type double, the return data type in the header must be double. The Ohio State University
pointDist2.cpp ... // main function int main() { double px, py, qx, qy, rx, ry; double dist_pq, dist_pr, dist_qr; // read input cout << "Enter point 1 (2 floats): "; cin >> px >> py; cout << "Enter point 2 (2 floats): "; cin >> qx >> qy; cout << "Enter point 3 (2 floats): "; cin >> rx >> ry; The Ohio State University
pointDist2.cpp // calculate distances dist_pq = dist(px, py, qx, qy); dist_pr = dist(px, py, rx, ry); dist_qr = dist(qx, qy, rx, ry); // output distances cout << "Distance between (" << px << "," << py << ") " << "and (" << qx <<"," << qy << ") = " << dist_pq << endl; cout << "Distance between (" << px << "," << py << ") " << "and (" << rx <<"," << ry << ") = " << dist_pr << endl; cout << "Distance between (" << qx << "," << qy << ") " << "and (" << rx <<"," << ry << ") = " << dist_qr << endl; return 0; } The Ohio State University
pointDist2.cpp ... // calculate distances dist_pq = dist(px, py, qx, qy); dist_pr = dist(px, py, rx, ry); dist_qr = dist(qx, qy, rx, ry); ... > pointDist2.exe Enter point 1 (2 floats): 0 0 Enter point 2 (2 floats): 3 0 Enter point 3 (2 floats): 0 4 Distance between (0,0) and (3,0) = 3 Distance between (0,0) and (0,4) = 4 Distance between (3,0) and (0,4) = 5 The Ohio State University
Outline of program pointDist2.cpp #include <iostream> #include <cmath> using namespace std; // function dist double dist(double x1, double y1, double x2, double y2) { // body of function dist . . . return(d); } // main function int main() { // body of the main function . . . return 0; } The Ohio State University
pointDist2.cpp // output distances // Note redundancy in these statements cout << "Distance between (" << px << "," << py << ") " << "and (" << qx <<"," << qy << ") = " << dist_pq << endl; cout << "Distance between (" << px << "," << py << ") " << "and (" << rx <<"," << ry << ") = " << dist_pr << endl; cout << "Distance between (" << qx << "," << qy << ") " << "and (" << rx <<"," << ry << ") = " << dist_qr << endl; return 0; } The Ohio State University
Function output_distance() // function output_distance // Note: return data type is void void output_distance(double x1, double y1, double x2, double y2, double distance) // five input parameters: x1, y1, x2, y2, distance // function output_distance returns nothing (void) { cout << "Distance between (" << x1 << "," << y1 << ") " << "and (" << x2 <<"," << y2 << ") = " << distance << endl; } The Ohio State University
pointDist3.cpp // include & namespace statements . . . // function definitions . . . // main function int main() { double px, py, qx, qy, rx, ry; double dist_pq, dist_pr, dist_qr; // read input cout << "Enter point 1 (2 floats): "; cin >> px >> py; cout << "Enter point 2 (2 floats): "; cin >> qx >> qy; cout << "Enter point 3 (2 floats): "; cin >> rx >> ry; The Ohio State University
pointDist3.cpp . . . // calculate distances dist_pq = dist(px, py, qx, qy); dist_pr = dist(px, py, rx, ry); dist_qr = dist(qx, qy, rx, ry); // output distances output_distance(px, py, qx, qy, dist_pq); output_distance(px, py, rx, ry, dist_pr); output_distance(qx, qy, rx, ry, dist_qr); return 0; } The Ohio State University
pointDist3.cpp > pointDist3.exe Enter point 1 (2 floats): 0 0 Enter point 2 (2 floats): 3 0 Enter point 3 (2 floats): 0 4 Distance between (0,0) and (3,0) = 3 Distance between (0,0) and (0,4) = 4 Distance between (3,0) and (0,4) = 5 5 4 3 The Ohio State University
C++ Functions • In C++, a function has four parts: • Name of the function • Data type(s) of the argument(s) • Data type of the return value • Body of the function • In general, the function template: returnDataType functionName(argument list) { //function body statement1; statement2; ... } Interface Implementation The Ohio State University
Advantages of functions From wikipedia.org: • Functions reduce duplication of code in a program; • Functions enable reuse of code across multiple programs (i.e., sqrt(), exp(), pow(),…) • Using functions, complex programs can be decomposed into simpler pieces; • Functions can hide/abstract parts of a program. The Ohio State University
Information Hiding • Adding is a function that takes 2 numbers a and b, and returns 1 number, the sum of a and b. • We do not care what happens inside the box. We just know that when we feed it a and b, we get the correct result! • This is how we have been using cmath functions such as pow() and sqrt(). See the convenience of functions? The Ohio State University
Information Hiding (2) • Abstraction: Separation of interface and implementation • When you were told to use pow() or sqrt(), I only told you about their usage interface. • For example, all you knew about pow() was that pow(x,y) returns the value xy • At no point in time were you ever interested in its algorithmic details (i.e. how exactly it gets xy) The Ohio State University
Abstraction • Why is abstraction useful? • Separates complexities of algorithm from its usage interface. It makes a programmer’s life easier! • A programmer who is simply interested in using this function should not need to worry about details of its implementation. The Ohio State University
Abstraction (2) • For instance, we interact with an ATM to withdraw, transfer, and query from our bank accounts. • We care about its interface: • How to interact with the ATM (input) • What it gives back to us (output) • But we don’t need to care about what goes on inside its machinery to make these things happen! • It is only the programmers of the complex software within the ATM that worry about its implementation. The Ohio State University
Abstraction (3) • Abstraction also allows for a transparent re-implementation of a function. • Let’s say someone wrote a function, pow2(x,y), which calculated xy the in half the time used by the cmath function pow(). • We can replace pow()’s implementation, and as long as we don’t alter the function’s usage interface, this change will be transparent across everyone’s code! • Any program using pow() will find that it now executes faster! The Ohio State University
Function computeCircleArea() // function to compute the area of a circle double computeCircleArea(double radius) { double area(0.0); // area of circle area = M_PI * radius * radius; return(area); } The Ohio State University
circleArea.cpp . . . // function computeCircleArea() definition . . . int main() { double area(0.0); // area of circle double radius(0.0); // radius of circle for (int i = 1; i <= 5; i++) { radius = i; // radius of circle area = computeCircleArea(radius); // call function cout << "Radius: " << radius << " Area: " << area << endl; } return 0; } The Ohio State University
… int main() { double area(0.0); // area of circle double radius(0.0); // radius of circle for (int i = 1; i <= 5; i++) { radius = i; // radius of circle area = computeCircleArea(radius); // call function cout << "Radius: " << radius << " Area: " << area << endl; } … > circleArea.exe Radius: 1 Area: 3.14159 Radius: 2 Area: 12.5664 Radius: 3 Area: 28.2743 Radius: 4 Area: 50.2655 Radius: 5 Area: 78.5398 The Ohio State University
Compute Cylinder Volume • Input: Radius of the circular base, Height. • Cylinder volume = (Circular base area) × Height Radius Height The Ohio State University
Function computeCylVolume() // function to compute the volume of a cylinder double computeCylVolume(double radius, double height) { double volume(0.0); // volume of cylinder double base_area(0.0); // area of cylinder base // call function computeCircleArea() base_area = computeCircleArea(radius); volume = base_area * height; return(volume); } The Ohio State University
cylinderVolume.cpp // include & namespace statements . . . // function computeCircleArea() definition double computeCircleArea(double radius) { . . . } // function computeCylVolume() definition double computeCylVolume(double radius, double height) { . . . } int main() { . . . return 0; } The Ohio State University
cylinderVolume.cpp ... int main() { double volume(0.0); // volume of cylinder double rad(0.0); // radius of circular base double h(0.0); // height of cylinder for (int i = 1; i <= 3; i++) { for (int j = 3; j <= 7; j += 2) { rad = i; // radius of circular base h = j; // height of cylinder volume = computeCylVolume(rad, h); // call function cout << "Radius: " << rad << “ Height: " << h << " Volume: " << volume << endl; } } ... The Ohio State University
for (int i = 1; i <= 3; i++) { for (int j = 3; j <= 7; j += 2) { rad = i; // radius of circular base h = j; // height of cylinder volume = computeCylVolume(rad, h); // call function cout << "Radius: " << rad << “ Height: " << h << " Volume: " << volume << endl; } } > cylinderVolume.exe Radius: 1 Height: 3 Volume: 9.42478 Radius: 1 Height: 5 Volume: 15.708 Radius: 1 Height: 7 Volume: 21.9911 Radius: 2 Height: 3 Volume: 37.6991 Radius: 2 Height: 5 Volume: 62.8319 Radius: 2 Height: 7 Volume: 87.9646 > The Ohio State University
circleArea.cpp . . . // function computeCircleArea() definition BEFORE main() . . . int main() { double area(0.0); // area of circle double radius(0.0); // radius of circle for (int i = 1; i <= 5; i++) { radius = i; // radius of circle area = computeCircleArea(radius); // call function cout << "Radius: " << radius << " Area: " << area << endl; } return 0; } The Ohio State University
circleAreaError.cpp // include & namespace statements . . . int main() { . . . for (int i = 1; i <= 5; i++) { radius = i; // radius of circle area = computeCircleArea(radius); // call function . . . } return 0; } // function computeCircleArea() definition double computeCircleArea(double radius) { . . . return(area); } The Ohio State University
for (int i = 1; i <= 5; i++) { radius = i; // radius of circle area = computeCircleArea(radius); // call function ... } // function computeCircleArea() definition double computeCircleArea(double radius) { ... return(area); } > g++ circleAreaError.cpp Compiling circleAreaError.cpp into circleAreaError.exe. circleAreaError.cpp: In function ‘int main()’: circleAreaError.cpp:17: error: ‘computeCircleArea’ was not declared in this scope > The Ohio State University
cylinderVolume.cpp // include & namespace statements . . . // function computeCircleArea() definition // BEFORE computeCylVolume() definition double computeCircleArea(double radius) { . . . } // function computeCylVolume() definition double computeCylVolume(double radius, double height) { . . . } int main() { . . . return 0; } The Ohio State University
cylinderVolumeError.cpp // include & namespace statements . . . // function computeCylVolume() definition double computeCylVolume(double radius, double height) { . . . } // function computeCircleArea() definition double computeCircleArea(double radius) { . . . } int main() { . . . return 0; } The Ohio State University
// function computeCylVolume() definition // BEFORE computeCircleArea() definition double computeCylVolume(double radius, double height) { . . . // call function computeCircleArea() base_area = computeCircleArea(radius); . . . } . . . // function computeCircleArea() definition double computeCircleArea(double radius) . . . > g++ cylinderVolumeError.cpp Compiling cylinderVolumeError.cpp into cylinderVolumeError.exe. cylinderVolumeError.cpp: In function ‘double computeCylVolume(double, double)’: cylinderVolumeError.cpp:15: error: ‘computeCircleArea’ was not declared in this scope > The Ohio State University
Function Prototype (1) • Like variables, before a function can be used, it must first be declared. • But this part is easy. The declaration simply consists of the function header that we have already seen. • The function declaration is also called a function prototype. The Ohio State University
circleArea2.cpp // include & namespace statements . . . // function computeCircleArea() prototype double computeCircleArea(double radius); int main() { . . . for (int i = 1; i <= 5; i++) { . . . area = computeCircleArea(radius); // call function . . . } return 0; } // function computeCircleArea() definition AFTER main() double computeCircleArea(double radius) { . . . return(area); } The Ohio State University
Function Prototype (2) • Prototypes describe a function’s interface! • What do each of the following tell us? double computeCircleArea(double radius); double dist(double x1, double y1, double x2, double y2); void output_distance(double x1, double y1, double x2, double y2, double distance); double average(int i, int j); double life_expectancy (int age, double height, double weight, bool sex); bool is_prime(int k); The Ohio State University
cylinderVolume2.cpp // include & namespace statements . . . // function prototypes // NOTE: Order here does not matter double computeCylVolume(double radius, double height); double computeCircleArea(double radius); int main() { . . . return 0; } // NOTE: Order no longer matters // function computeCylVolume() definition . . . // function computeCircleArea() definition . . . The Ohio State University
Function Prototype (3) • Function prototypes can be placed: • Anywhere above where the function will be called (just like variables) • In general, at least for this course, just place prototypes above int main(). The Ohio State University
Function Prototype (4) double dist(double x1, double y1, double x2, double y2); double life_expectancy (int age, double height, double weight, bool sex); • Alternative function prototype format (used by text): // Note: Variable name is omitted double dist(double, double, double, double); double life_expectancy(int, double, double, bool); The Ohio State University
Function Definition • After declaring the function prototype, we have to define what it does. • In this course, you should place all function definitions after main(). The Ohio State University
#include <iostream> using namespace std; int maxInt(int a, int b); int main() { cout << maxInt(88, 102) << endl; return 0; } int maxInt(int a, int b) { if (a >= b) { return a; } else { return b; } } Function Prototype Function Call Function Definition The Ohio State University
pointDist4.cpp // include & namespace statements . . . // function protoypes double dist(double x1, double y1, double x2, double y2); void output_distance(double x1, double y1, double x2, double y2, double distance); // main function int main() { . . . } // function definitions . . . The Ohio State University