73
CSE202: Lecture 11 The Ohio State University 1 Functions

CSE202: Lecture 11The Ohio State University1 Functions

Embed Size (px)

Citation preview

Page 1: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 1

Functions

Page 2: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 2

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;

Page 3: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 3

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);...

Page 4: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 4

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;}

Page 5: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 5

pointDist1.cpp (3)

> pointDist1.exeEnter point 1 (2 floats): 0 0Enter point 2 (2 floats): 3 0Enter point 3 (2 floats): 0 4Distance between (0,0) and (3,0) = 3Distance between (0,0) and (0,4) = 4Distance between (3,0) and (0,4) = 5

4

3

5

Page 6: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 6

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);...

Page 7: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 7

Functions

Examples of C++ functions:

• sqrt(a)

• exp(a)

• cos(a)

• pow(a,b)

Page 8: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 8

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.”

Page 9: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 9

Function dist()

// function distdouble 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);}

Page 10: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 10

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

Page 11: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 11

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.

Page 12: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 12

pointDist2.cpp...// main functionint 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;

Page 13: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 13

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;}

Page 14: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 14

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.exeEnter point 1 (2 floats): 0 0Enter point 2 (2 floats): 3 0Enter point 3 (2 floats): 0 4Distance between (0,0) and (3,0) = 3Distance between (0,0) and (0,4) = 4Distance between (3,0) and (0,4) = 5

Page 15: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 15

Outline of program pointDist2.cpp#include <iostream>#include <cmath>using namespace std;

// function distdouble dist(double x1, double y1, double x2, double y2){ // body of function dist . . . return(d);}

// main functionint main(){ // body of the main function . . . return 0;}

Page 16: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 16

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;}

Page 17: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 17

Function output_distance()

// function output_distance// Note: return data type is voidvoid 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;}

Page 18: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 18

pointDist3.cpp// include & namespace statements. . .// function definitions. . .// main functionint 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;

Page 19: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 19

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;}

Page 20: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 20

pointDist3.cpp

> pointDist3.exeEnter point 1 (2 floats): 0 0Enter point 2 (2 floats): 3 0Enter point 3 (2 floats): 0 4Distance between (0,0) and (3,0) = 3Distance between (0,0) and (0,4) = 4Distance between (3,0) and (0,4) = 5

4

3

5

Page 21: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 21

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 bodystatement1;statement2;...

}

Interface

Implementation

Page 22: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 22

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.

Page 23: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 23

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?

Add

a b

a + b

Page 24: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 24

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)

Page 25: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 25

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.

Page 26: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 26

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.

Page 27: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 27

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!

Page 28: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 28

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);

}

Page 29: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 29

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;

}

Page 30: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 30

> 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; }

Page 31: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 31

Compute Cylinder Volume

• Input: Radius of the circular base,Height.

• Cylinder volume = (Circular base area) × Height

HeightRadius

Page 32: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 32

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);

}

Page 33: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 33

cylinderVolume.cpp// include & namespace statements. . .// function computeCircleArea() definitiondouble computeCircleArea(double radius){ . . . }

// function computeCylVolume() definitiondouble computeCylVolume(double radius, double height){ . . . }

int main(){ . . . return 0;}

Page 34: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 34

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; } }...

Page 35: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 35

> cylinderVolume.exeRadius: 1 Height: 3 Volume: 9.42478Radius: 1 Height: 5 Volume: 15.708Radius: 1 Height: 7 Volume: 21.9911Radius: 2 Height: 3 Volume: 37.6991Radius: 2 Height: 5 Volume: 62.8319Radius: 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; } }

Page 36: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 36

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;

}

Page 37: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 37

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() definitiondouble computeCircleArea(double radius){ . . . return(area);}

Page 38: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 38

> 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 circle17. area = computeCircleArea(radius); // call function ...24. }25. 26. // function computeCircleArea() definition27. double computeCircleArea(double radius)28. { ...31. return(area);32. }

Page 39: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 39

cylinderVolume.cpp// include & namespace statements. . .// function computeCircleArea() definition// BEFORE computeCylVolume() definitiondouble computeCircleArea(double radius){ . . . }

// function computeCylVolume() definitiondouble computeCylVolume(double radius, double height){ . . . }

int main(){ . . . return 0;}

Page 40: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 40

cylinderVolumeError.cpp// include & namespace statements. . .// function computeCylVolume() definitiondouble computeCylVolume(double radius, double height){ . . . }

// function computeCircleArea() definitiondouble computeCircleArea(double radius){ . . . }

int main(){ . . . return 0;}

Page 41: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 41

> 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() definition8. // BEFORE computeCircleArea() definition9. double computeCylVolume(double radius, double height)10. { . . .14. // call function computeCircleArea()15. base_area = computeCircleArea(radius); . . . }. . .21. // function computeCircleArea() definition22. double computeCircleArea(double radius). . .

Page 42: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 42

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.

Page 43: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 43

circleArea2.cpp// include & namespace statements. . .// function computeCircleArea() prototypedouble 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);}

Page 44: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 44

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);

Page 45: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 45

cylinderVolume2.cpp// include & namespace statements. . .// function prototypes// NOTE: Order here does not matterdouble computeCylVolume(double radius, double height);double computeCircleArea(double radius);

int main(){ . . . return 0;}

// NOTE: Order no longer matters// function computeCylVolume() definition. . .// function computeCircleArea() definition. . .

Page 46: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 46

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().

Page 47: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 47

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);

Page 48: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 48

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().

Page 49: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 49

#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

Page 50: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 50

pointDist4.cpp// include & namespace statements. . .// function protoypesdouble dist(double x1, double y1, double x2, double y2);void output_distance(double x1, double y1, double x2, double y2, double distance);

// main functionint main(){. . .}

// function definitions. . .

Page 51: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 51

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);}

Page 52: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 52

> 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 circle17. area = computeCircleArea(radius); // call function . . .23. } . . .26. double computeCircleArea(double radius)27. { . . .31. return(area);32. }

Page 53: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 53

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);}

Page 54: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 54

> 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 circle17. area = computeCircleArea(radius); // call function . . .23. } . . .26. double computeCircleArea(double radius)27. { . . .31. return(area);32. }

Page 55: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 55

distance.cpp. . .// function prototypedouble 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;}

Page 56: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 56

distance.cpp. . .// function to compute distance to origindouble computeDistance(double x, double y){ double d(0.0);

d = sqrt(x*x + y*y); return(d);}

Page 57: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 57

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);}

Page 58: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 58

> 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);}

Page 59: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 59

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)

Page 60: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 60

Functions in Expressions

double dist(double x1, double y1, double x2, double y2);

• Functions can appear in expressions:

// perfectly validdouble z = dist(1.0, 0.0, 0.0, 1.0) + 2;

• Expressions can be arguments to functions:

// also validdouble x1 = 6.0, x2 = 3.0; double z = dist(x2 – x1, 2.0*3, x1*2, 2.0+4.0);

Page 61: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 61

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);

Page 62: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 62

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);

Page 63: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 63

#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; }

}

Page 64: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 64

What is wrong with this program?#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; }

}

Page 65: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 65

What is wrong with this program?#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; }

}

Page 66: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 66

Updated Layout of Your Program

preprocessor directives

function prototypes

int main(){

constant definitions (ALL CAPS)variable declarations

other statements

return 0;}

function definitions

Page 67: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 67

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){...}

Page 68: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 68

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){...}

Page 69: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 69

The void Data Type

Page 70: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 70

Function output_distance()

// function output_distance// Note: return data type is voidvoid 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;}

Page 71: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 71

void Data Type

• void means the absence of information.

• If the return data type is void, then the function does not return any value.

Page 72: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 72

Function read_age()

// function read_age// Prompt, read and return user’s age.// Note: No argumentsint read_age(){

int age(0);

cout << "Please enter your age: ";cin >> age;while (age < 0 || age > 120) {

cout << "Age must be between 0 and 120." << endl; cout << "Please enter your age: "; cin >> age; } return(age);}

Page 73: CSE202: Lecture 11The Ohio State University1 Functions

CSE202: Lecture 11 The Ohio State University 73

Functions without Arguments

• A function may not need any arguments. • Functions which read in data sometimes have

no arguments, i.e.:

int read_age()

{

...

}