Download presentation
Presentation is loading. Please wait.
Published byDavid Shelton Modified over 9 years ago
1
CSE202: Lecture 11The Ohio State University1 Functions
2
CSE202: Lecture 11The Ohio State University2 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;
3
CSE202: Lecture 11The Ohio State University3 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);...
4
CSE202: Lecture 11The Ohio State University4 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; }
5
CSE202: Lecture 11The Ohio State University5 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 4 3 5
6
CSE202: Lecture 11The Ohio State University6 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);...
7
CSE202: Lecture 11The Ohio State University7 Functions Examples of C++ functions: sqrt(a) exp(a) cos(a) pow(a,b)
8
CSE202: Lecture 11The Ohio State University8 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.”
9
CSE202: Lecture 11The Ohio State University9 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); }
10
CSE202: Lecture 11The Ohio State University10 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 double says 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
11
CSE202: Lecture 11The Ohio State University11 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.
12
CSE202: Lecture 11The Ohio State University12 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;
13
CSE202: Lecture 11The Ohio State University13 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; }
14
CSE202: Lecture 11The Ohio State University14 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
15
CSE202: Lecture 11The Ohio State University15 Outline of program pointDist2.cpp #include 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; }
16
CSE202: Lecture 11The Ohio State University16 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; }
17
CSE202: Lecture 11The Ohio State University17 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; }
18
CSE202: Lecture 11The Ohio State University18 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;
19
CSE202: Lecture 11The Ohio State University19 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; }
20
CSE202: Lecture 11The Ohio State University20 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 4 3 5
21
CSE202: Lecture 11The Ohio State University21 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
22
CSE202: Lecture 11The Ohio State University22 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.
23
CSE202: Lecture 11The Ohio State University23 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?
24
CSE202: Lecture 11The Ohio State University24 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 x y –At no point in time were you ever interested in its algorithmic details (i.e. how exactly it gets x y )
25
CSE202: Lecture 11The Ohio State University25 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.
26
CSE202: Lecture 11The Ohio State University26 Abstraction (2) For instance, we interact with an ATM to withdraw, transfer, and query from our bank accounts. We care about its interface: 1.How to interact with the ATM (input) 2.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.
27
CSE202: Lecture 11The Ohio State University27 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 x y 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!
28
CSE202: Lecture 11The Ohio State University28 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); }
29
CSE202: Lecture 11The Ohio State University29 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; }
30
CSE202: Lecture 11The Ohio State University30 > 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 … 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; } …
31
CSE202: Lecture 11The Ohio State University31 Compute Cylinder Volume Input: Radius of the circular base, Height. Cylinder volume = (Circular base area) × Height Height Radius
32
CSE202: Lecture 11The Ohio State University32 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); }
33
CSE202: Lecture 11The Ohio State University33 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; }
34
CSE202: Lecture 11The Ohio State University34 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; }...
35
CSE202: Lecture 11The Ohio State University35 > 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 > 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; }
36
CSE202: Lecture 11The Ohio State University36 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; }
37
CSE202: Lecture 11The Ohio State University37 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); }
38
CSE202: Lecture 11The Ohio State University38 > 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 > 14.for (int i = 1; i <= 5; i++) 15. { 16. radius = i; // radius of circle 17. area = computeCircleArea(radius); // call function... 24. } 25. 26.// function computeCircleArea() definition 27.double computeCircleArea(double radius) 28.{... 31. return(area); 32.}
39
CSE202: Lecture 11The Ohio State University39 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; }
40
CSE202: Lecture 11The Ohio State University40 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; }
41
CSE202: Lecture 11The Ohio State University41 > 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 > 7.// function computeCylVolume() definition 8.// BEFORE computeCircleArea() definition 9.double computeCylVolume(double radius, double height) 10.{... 14. // call function computeCircleArea() 15. base_area = computeCircleArea(radius);... }... 21.// function computeCircleArea() definition 22.double computeCircleArea(double radius)...
42
CSE202: Lecture 11The Ohio State University42 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.
43
CSE202: Lecture 11The Ohio State University43 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); }
44
CSE202: Lecture 11The Ohio State University44 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);
45
CSE202: Lecture 11The Ohio State University45 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...
46
CSE202: Lecture 11The Ohio State University46 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().
47
CSE202: Lecture 11The Ohio State University47 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);
48
CSE202: Lecture 11The Ohio State University48 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().
49
CSE202: Lecture 11The Ohio State University49 #include 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
50
CSE202: Lecture 11The Ohio State University50 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...
51
CSE202: Lecture 11The Ohio State University51 What’s the error?... void computeCircleArea(double radius);... int main() {... for (int i = 1; i <= 5; i++) { radius = i; // radius of circle area = computeCircleArea(radius); // call function... }... } double computeCircleArea(double radius) {... return(area); }
52
CSE202: Lecture 11The Ohio State University52 > g++ circleAreaError2.cpp Compiling circleAreaError2.cpp into circleAreaError2.exe. circleAreaError2.cpp: In function ‘int main()’: circleAreaError2.cpp:17: error: void value not ignored as it ought to be circleAreaError2.cpp: In function ‘double computeCircleArea(double)’: circleAreaError2.cpp:26: error: new declaration ‘double computeCircleArea(double)’ circleAreaError2.cpp:8: error: ambiguates old declaration ‘void computeCircleArea(double)’ > 8.void computeCircleArea(double radius);... 14. for (int i = 1; i <= 5; i++) 15. { 16. radius = i; // radius of circle 17. area = computeCircleArea(radius); // call function... 23. }... 26.double computeCircleArea(double radius) 27.{... 31. return(area); 32.}
53
CSE202: Lecture 11The Ohio State University53 What’s the error?... double computeCircleArea(int radius);... int main() {... for (int i = 1; i <= 5; i++) { radius = i; // radius of circle area = computeCircleArea(radius); // call function... }... } double computeCircleArea(double radius) {... return(area); }
54
CSE202: Lecture 11The Ohio State University54 > g++ circleAreaError3.cpp Compiling circleAreaError3.cpp into circleAreaError3.exe. /tmp/ccV2a0SZ.o: In function `main': circleAreaError3.cpp:(.text+0x30): undefined reference to `computeCircleArea(int)' collect2: ld returned 1 exit status > 8.double computeCircleArea(int radius);... 14. for (int i = 1; i <= 5; i++) 15. { 16. radius = i; // radius of circle 17. area = computeCircleArea(radius); // call function... 23. }... 26.double computeCircleArea(double radius) 27.{... 31. return(area); 32.}
55
CSE202: Lecture 11The Ohio State University55 distance.cpp... // function prototype double computeDistance(double x, double y); int main() { double x(0.0), y(0.0); cout << "Enter x, y: "; cin >> x >> y; cout << "Distance to origin = " << computeDistance(x,y) << endl; return 0; }
56
CSE202: Lecture 11The Ohio State University56 distance.cpp... // function to compute distance to origin double computeDistance(double x, double y) { double d(0.0); d = sqrt(x*x + y*y); return(d); }
57
CSE202: Lecture 11The Ohio State University57 What’s the error?... double computeDistance(double x, double y); int main() {... cout << "Distance to origin = " << computeDistance(x,y) << endl;... } double computeDistance(double x, double y, double z) { double d(0.0); d = sqrt(x*x + y*y + z*z); return(d); }
58
CSE202: Lecture 11The Ohio State University58 > g++ distanceError.cpp Compiling distanceError.cpp into distanceError.exe. /tmp/cccKGvqW.o: In function `main': distanceError.cpp:(.text+0x54): undefined reference to `computeDistance(double, double)' collect2: ld returned 1 exit status > double computeDistance(double x, double y);... cout << "Distance to origin = " << computeDistance(x,y) << endl;... double computeDistance(double x, double y, double z) {... d = sqrt(x*x + y*y + z*z); return(d); }
59
CSE202: Lecture 11The Ohio State University59 Function Prototype The function prototype should look EXACTLY LIKE the function header (except that the prototype ends with a ‘;’.) Protoype: double computeDistance(double x, double y); Header: double computeDistance(double x, double y)
60
CSE202: Lecture 11The Ohio State University60 Functions in Expressions double dist(double x1, double y1, double x2, double y2); Functions can appear in expressions: // perfectly valid double z = dist(1.0, 0.0, 0.0, 1.0) + 2; Expressions can be arguments to functions: // also valid double x1 = 6.0, x2 = 3.0; double z = dist(x2 – x1, 2.0*3, x1*2, 2.0+4.0);
61
CSE202: Lecture 11The Ohio State University61 Invalid Function Calls double dist (double x1, double y1, double x2, double y2); double average(int i, int j); double life_expectancy (int age, double height, double weight, bool sex); These function calls are not valid. Why not? double z = dist(2.0, 0.0, 0.0); double z = average(3, 5, 9); double life = life_expectancy(22, false, 5.5, 140.2);
62
CSE202: Lecture 11The Ohio State University62 More Invalid Function Calls void output_distance(double x1, double y1, double x2, double y2, double distance); Why is this function call invalid? int k = output_distance(3.0, 0.0, 0.0, 4.0, 5.0);
63
CSE202: Lecture 11The Ohio State University63 #include 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; } }
64
CSE202: Lecture 11The Ohio State University64 What is wrong with this program? #include 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; } }
65
CSE202: Lecture 11The Ohio State University65 What is wrong with this program? #include 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; } }
66
CSE202: Lecture 11The Ohio State University66 Updated Layout of Your Program preprocessor directives function prototypes int main() { constant definitions (ALL CAPS) variable declarations other statements return 0; } function definitions
67
CSE202: Lecture 11The Ohio State University67 Documenting Functions EVERY function should have a comment explaining what it does; EVERY function parameter should have a comment explaining the parameter. Example: // Return the distance from (x1,y1) to (x2,y2). // x1 = x-coordinates of first point. // y1 = y-coordinates of first point. // x2 = x-coordinates of second point. // y2 = y-coordinates of second point. double dist(double x1, double y1, double x2, double y2) {... }
68
CSE202: Lecture 11The Ohio State University68 Documenting Functions (2) Example of documenting function: // Output the distance from (x1,y1) to (x2,y2). // x1 = x-coordinates of first point. // y1 = y-coordinates of first point. // x2 = x-coordinates of second point. // y2 = y-coordinates of second point. // distance = distance from (x1,y1) to (x2,y2). void output_distance(double x1, double y1, double x2, double y2, double distance) {... }
69
CSE202: Lecture 11The Ohio State University69 The void Data Type
70
CSE202: Lecture 11The Ohio State University70 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; }
71
CSE202: Lecture 11The Ohio State University71 void Data Type void means the absence of information. If the return data type is void, then the function does not return any value.
72
CSE202: Lecture 11The Ohio State University72 Function read_age() // function read_age // Prompt, read and return user’s age. // Note: No arguments int read_age() { int age(0); cout << "Please enter your age: "; cin >> age; while (age 120) { cout << "Age must be between 0 and 120." << endl; cout << "Please enter your age: "; cin >> age; } return(age); }
73
CSE202: Lecture 11The Ohio State University73 Functions without Arguments A function may not need any arguments. Functions which read in data sometimes have no arguments, i.e.: int read_age() {... }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.