(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Spring Semester 2013 Lecture 5
Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Functions (II) H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 24, 2011) Washington State University.
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Software Development Method & C Language Elements H&K Chapter 1-2
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Overview creating your own functions calling your own functions.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Guide To UNIX Using Linux Third Edition
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
(1 - 2) C Language Elements H&K Chapter 2 Instructor - Andrew S. O’Fallon CptS 121 (August 28, 2015) Washington State University.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(3-2) File Processing with Functions Instructor - Andrew S. O’Fallon CptS 121 (September 11, 2015) Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
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.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1 ICS103 Programming in C Lecture 8: Functions I.
CISC105 – General Computer Science Class 2 – 6/7/2006.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
Chapter 9: Value-Returning Functions
Chapter 7: User-Defined Functions II
Lesson #6 Modular Programming and Functions.
Chapter 6: Modular Programming
Lesson #6 Modular Programming and Functions.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
User-Defined Functions
Lesson #6 Modular Programming and Functions.
Chapter 7: User-Defined Functions II
Lesson #6 Modular Programming and Functions.
CPS125.
Presentation transcript:

(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University

C. Hundhausen, A. O’Fallon 2 Overview of Functions (1) A block of code or unit that generally performs one task – Cohesion is defined as a measure of how focused and related the functionality is within a single unit – We strive for “high” cohesion in our functions and programs – Highly cohesive functions and programs are more readable, testable, reusable, understandable, and maintainable The unit may be invoked from many different parts of a program

C. Hundhausen, A. O’Fallon 3 Overview of Functions (2) Functions should be “loosely” coupled – Coupling is another way to indicate functions are dependent on one another We strive for highly cohesive and loosely coupled components in our programs May reduce the length of a program Promote more efficient debugging of a program

C. Hundhausen, A. O’Fallon 4 Another Example (1) Problem Statement: Vehicles crossing the Hiawatha bridge must pay a toll. Write a tollbooth application that computes a vehicle's toll. A vehicle is charged $1.00 per axle, plus a surcharge based on its weight. The surcharge is $.50 per ton or fraction thereof. Write a program that computes the tolls of three vehicles crossing the bridge. You should prompt the user for the name of the file containing the data on the three vehicles. Read in the data from that file, and display the toll that is due for each vehicle. Note that one ton is equivalent to 2000 pounds. Inputs: – infile_name – num_axles – weight Outputs – toll_due Relevant formula: toll = (num_axles * 1.00) + (0.5 * ceil(weight/2000.0))

C. Hundhausen, A. O’Fallon 5 Another Example (2) Initial algorithm – Get name of the file containing the vehicle data and open the file – Compute the tolls of the three vehicles – Display the results Refined algorithm – Get the name of the file containing the vehicle data and open the file – Read in the number of axles and weight of each of the three vehicles – Compute the tolls of the three vehicles toll = (num_axles * 1.00) + (0.5 * ceil(weight/2000.0)) – Display the results

C. Hundhausen, A. O’Fallon 6 Another Example (3) Structure chart Compute tolls of three vehicles Get the file name and open the file Read data from file Compute toll get_and_open_file()read_num_axles() read_weight() compute_toll() Display results display_toll()

C. Hundhausen, A. O’Fallon 7 Another Example (4) Implementation /* * Computes the tolls of three vehicles. */ #include /* printf, scanf defs */ #include /* for ceil() */ /* Function prototypes */ FILE * get_and_open_file (void); /* prompts user for name of file & opens file for reading */ int read_num_axles (FILE *); /* reads number of axles from file */ double read_weight (FILE *); /* reads weight from file */ double compute_toll (int, double); /* computes the toll */ void display_toll (int, double, double); /* displays number of axles, weight, and corresponding toll. */ int main (void) { FILE *infile = NULL; int axles1 = 0, axles2 = 0, axles3 = 0; double weight1 = 0.0, weight2 = 0.0, weight3 = 0.0, toll1 = 0.0, toll2 = 0.0, toll3 = 0.0; infile = get_and_open_file (); axles1 = read_num_axles (infile); weight1 = read_weight (infile);

C. Hundhausen, A. O’Fallon 8 Another Example (5) Implementation (cont.) axles2 = read_num_axles (infile); weight2 = read_weight (infile); axles3 = read_num_axles (infile); weight3 = read_weight (infile); toll1 = compute_toll (axles1,weight1); toll2 = compute_toll (axles2, weight2); toll3 = compute_toll (axles3, weight3); display_toll (axles1, weight1, toll1); display_toll (axles2, weight2, toll2); display_toll (axles3, weight3, toll3); return 0; }

C. Hundhausen, A. O’Fallon 9 Another Example (6) A note on function comments – Good style: Document the pre- and post- conditions of the function – Preconditions: assumed to be true prior to executing the function – Postconditions: assumed to be true after the function is done executing – Style: * read_int: reads an integer from file foo * Pre: File foo is open for reading Next item to be read from foo is an integer * Post: Return value contains value read in

C. Hundhausen, A. O’Fallon 10 Another Example (7) A note on reading in file names – The char * (pointer to array of characters) data type is used to store strings of characters, e.g., "infile.dat" – You can define a string of characters as follows: char infile_name[30] /* a character string of length 30 */ – You can use scanf to read in a string of characters with the %s placeholder: scanf ("%s", infile_name); /* read in name of input file */

C. Hundhausen, A. O’Fallon 11 You Try It (1) Definition of get_and_open_file /* get_and_open_file – prompts user for name of file, and opens the file for reading. Pre: None, but assume a valid filename will be entered Post: return value contains a pointer to the file, which is open for reading. */ FILE * get_and_open_file() { /* TO DO: Fill in code here */ }

C. Hundhausen, A. O’Fallon 12 You Try It (2) Definition of read_num_axles /* read_num_axles – reads integer from input file Pre: in_file is open for reading, and the next item to be read in is an integer Post: return value contains integer value read in */ int read_num_axles(FILE *infile) { /* TO DO: Fill in code here */ }

C. Hundhausen, A. O’Fallon 13 You Try It (3) Definition of read_weight /* read_weight – reads floating point weight from input file Pre: in_file is open for reading, and the next item to be read in is a double value Post: return value contains double value read in */ double read_weight(FILE *in_file) { /* TO DO: Fill in code here */ }

C. Hundhausen, A. O’Fallon 14 You Try It (4) Definition of compute_toll /* compute_toll – reads a vehicles toll based on number of axles and weight. Pre: num_axles is < 0, and weight is < 0 Post: return value contains correct toll */ double compute_toll(int num_axles, double weight) { /* TO DO: Fill in code here */ }

C. Hundhausen, A. O’Fallon 15 You Try It (5) Definition of display_toll /* display_toll – displays a vehicles toll Pre: None Post: Toll is pretty-printed to display. */ void display_ toll(int num_axles, double weight, double toll) { /* TO DO: Fill in code here */ }

C. Hundhausen, A. O’Fallon 16 Notes on Example (1) Local versus global variables – Global variables Declared outside of a function (usually after the #define and #include statements) – Local variables Declared within a function Only visible from within that function; once function is done, variables go away (space is deallocated) Local variables are by default considered automatic variables; auto may be placed in front of these variables but is not required – Notice: NO GLOBAL VARIABLES! In general, they’re a bad idea Why?

C. Hundhausen, A. O’Fallon 17 Notes on Example (2) Actual arguments must match formal parameters – OK to pass in actual argument of type double for formal argument of type int (no data loss) – Probably not OK to pass in actual argument of type int for formal argument of type double (possible data loss) Correspondence rules – Number of actual arguments must match number of formal parameters – Order of arguments determines correspondence – Data types of actual arguments should match those of formal parameters, or at least the types passed in should not cause data loss

C. Hundhausen, A. O’Fallon 18 Independent Function Testing (1) Each function is itself a small-scale "program" – It has inputs – It has expected outputs or side-effects – Ideally, it is a self-contained "black box" (does not manipulate global variables) It makes sense to test each function independently, so that its correctness can be verified before it is used in a larger scale application A test-driver is a short program that tests a specific function

C. Hundhausen, A. O’Fallon 19 Independent Function Testing (2) Example: Test driver for compute_toll : #include double compute_toll (int,double); /* prototype */ int main (void) /* test driver */ { int num_axles = 0; double weight = 0.0; printf ("Enter the number of axles: "); scanf ("%d", &num_axles); printf ("Enter the weight: "); scanf ("%lf", &weight); printf ("The computed toll is $%.2f.", compute_toll (num_axles, weight)); return 0; }

C. Hundhausen, A. O’Fallon 20 Common Programming Errors Forgetting to include proper #include directives Not matching actual arguments to formal parameters (remember, number, order, and type matter) Calling a function with input data for which the function is undefined

C. Hundhausen, A. O’Fallon 21 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8 th Ed.), Addison- Wesley, 2016

C. Hundhausen, A. O’Fallon 22 Collaborators Chris Hundhausen