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

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

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

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

(3-1) Functions IIH&K Chapter 3

Instructor - Andrew S. O’Fallon

CptS 121 (September 9, 2015)

Washington State University

Page 2: (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’Fallon2

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

Page 3: (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’Fallon3

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

Page 4: (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’Fallon4

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

Page 5: (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’Fallon5

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

Page 6: (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’Fallon6

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

Page 7: (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’Fallon7

Another Example (4)

Implementation/* * Computes the tolls of three vehicles. */

#include <stdio.h> /* printf, scanf defs */#include <math.h> /* 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);

Page 8: (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’Fallon8

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

Page 9: (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’Fallon9

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

Page 10: (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’Fallon10

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 */

Page 11: (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’Fallon11

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 */}

Page 12: (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’Fallon12

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 */

}

Page 13: (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’Fallon13

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 */

}

Page 14: (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’Fallon14

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 */

}

Page 15: (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’Fallon15

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 */

}

Page 16: (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’Fallon16

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?

Page 17: (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’Fallon17

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

Page 18: (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’Fallon18

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

Page 19: (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’Fallon19

Independent Function Testing (2)

Example: Test driver for compute_toll:

#include <stdio.h>#include <math.h>

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

Page 20: (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’Fallon20

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

Page 21: (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’Fallon21

References

J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8th Ed.), Addison-Wesley, 2016

Page 22: (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’Fallon22

Collaborators

Chris Hundhausen