CSE202: Lecture 11The Ohio State University1 Functions.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

CSE202: Lecture 12The Ohio State University1 Function Calling.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Chapter 6: Functions.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
1 CS161 Introduction to Computer Science Topic #9.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Repetitive Structures
User-Written Functions
Chapter 6: User-Defined Functions I
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
CSCI 161: Introduction to Programming Function
User-Defined Functions
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Announcements Homework 1 will be assigned this week,
Chapter 6: User-Defined Functions I
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Functions
Presentation transcript:

CSE202: Lecture 11The Ohio State University1 Functions

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;

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

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

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

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

CSE202: Lecture 11The Ohio State University7 Functions Examples of C++ functions: sqrt(a) exp(a) cos(a) pow(a,b)

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

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

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

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.

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;

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

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

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

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

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

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;

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

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

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

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.

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?

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 )

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.

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.

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!

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

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

CSE202: Lecture 11The Ohio State University30 > circleArea.exe Radius: 1 Area: Radius: 2 Area: Radius: 3 Area: Radius: 4 Area: Radius: 5 Area: … 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; } …

CSE202: Lecture 11The Ohio State University31 Compute Cylinder Volume Input: Radius of the circular base, Height. Cylinder volume = (Circular base area) × Height Height Radius

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

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

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

CSE202: Lecture 11The Ohio State University35 > cylinderVolume.exe Radius: 1 Height: 3 Volume: Radius: 1 Height: 5 Volume: Radius: 1 Height: 7 Volume: Radius: 2 Height: 3 Volume: Radius: 2 Height: 5 Volume: Radius: 2 Height: 7 Volume: > 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; }

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

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

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 } // function computeCircleArea() definition 27.double computeCircleArea(double radius) 28.{ return(area); 32.}

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

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

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.{ // call function computeCircleArea() 15. base_area = computeCircleArea(radius);... } // function computeCircleArea() definition 22.double computeCircleArea(double radius)...

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.

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

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

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

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

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

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

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

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

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

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); for (int i = 1; i <= 5; i++) 15. { 16. radius = i; // radius of circle 17. area = computeCircleArea(radius); // call function } double computeCircleArea(double radius) 27.{ return(area); 32.}

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

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); for (int i = 1; i <= 5; i++) 15. { 16. radius = i; // radius of circle 17. area = computeCircleArea(radius); // call function } double computeCircleArea(double radius) 27.{ return(area); 32.}

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

CSE202: Lecture 11The Ohio State University69 The void Data Type

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

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.

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

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