36
Copyright © 2002 W. A. Tu cker 1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Embed Size (px)

Citation preview

Page 1: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 1

Chapter 3 Lecture Notes

Bill Tucker

Austin Community College

COSC 1315

Page 2: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 2

Top Down Design

• “Where do I begin” is often the question when faced with a complex problem

• Top Down Design usually helps break the problem into smaller, more digestible, chunks

• EX: “How would you find the weight of a batch of washers?”

Page 3: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 3

Algorithm

• Get required input from user

• Calculate weight of the batch of washers

• Display the weight of the batch of washers

Page 4: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 4

Washer ProblemWeight of batch = quantity x unit weight

Find the weight of the washer weight = density x rim volume

Rim volume = rim area x thickness

Find the rim area rim area = area of outer circle -

area of inner circle

area of a circle = PI * radius2

radius = diameter / 2

Required Input: quantity outer diameter hole diameter thickness density

Page 5: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 5

Algorithm

• Get required input from user – Get quantity– Get outer diameter and hole diameter– Get thickness and density

• Calculate the weight of the batch of washers– Calculate area of outer circle and area of inner circle

(same algorithm for both)– Calculate rim area and rim volume– Calculate washer weight– Calculate weight of the batch of washers

• Display the weight of the batch of washers

Page 6: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 6

Problem Analysis Results

• Problem approach has been determined without worrying about the HOW, or implementation details

• Each one of these steps (tasks or procedures) could be implemented separately, then combined together

• This process is called modularization • The resulting code is called a function (C++) or a

method (Java)

Page 7: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 7

Advantages of Functions / Methods

• Breaks the problem down into smaller, more manageable chunks

• Allows multiple programmers to work on the task at the same time

• Promotes reuse of code – The code to calculate the area of a circle only

needs to be done once, then used twice, with different data (diameters)

Page 8: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 8

Types of Functions

• Functions are either – built in functions OR – programmer defined functions

• Functions may or may not have values passed to them

• Functions may or may not return a value to the calling function

Page 9: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 9

Built in Math Functions• There are many mathematical functions that are

provided with the compiler• #include <cmath> provides access to these

functions• A function call is used to pass values to these

functions and receive the results– result = pow(number, 2); // result will contain the

square of the value of number– answer = sqrt (number); // answer will contain the

square root of the value of number

• Most functions return values of data type double

Page 10: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 10

Defining a Function / Method• A name must be created

– Use the same rules as naming any value– Must be unique, may not be the same as any other name– Usually starts with a verb, since some action is done

• An IPO chart may be created to document the function / method

• A special flowchart symbol may be used for a predefined procedure to represent a function / method in the mainline function

Page 11: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 11

Washer Problem Flowchartstart

Get quantity

Get outer diamGet hole diam

Get thicknessGet density

calcArea outer circle

calcAreainner circle

Calculate rim arearim vol, rim weightweight of batch

Display weightof batch

stop

Page 12: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 12

Flowchart for calcArea function

start calcArea

radius = diameter / 2

area = PI * radius * radius

return area

Input parameter diameter

Processing value PI = 3.14159

Page 13: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 13

Programmer Defined Functions• Adding a programmer defined function to a

program requires THREE elements– Function prototype

• Defines the function name to the compiler (tells the compiler what is coming)

• Defines the communication to and from the function – Function call

• Transfers control to the function for execution• Specifies arguments that are to be passed to the function• Receives the one value that may be “returned”

– Function definition• The actual code that is executed when the function is called

Page 14: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 14

Function Communication

No InputNo Output

InputNo Output

No InputOutput

Output Input

Page 15: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 15

Function with no input or output

No InputNo Output printLine

Prototype: void printLine();

Call: printLine();

Definition: void printLine() { cout << “----------------------------”; cout << endl; } // end printLine

Page 16: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 16

Function with input only

areaNo Output printValue

Prototype: void printValue(double);

Call: double area; printValue(area);

Definition: void printValue(double area) { cout << “value is “; cout << area; cout << endl; } // end printLine

Page 17: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 17

Function with output only

No Inputdiameter getValue

Prototype: double getValue();

Call: double diameter; diameter = getValue();

Definition: double getValue() { double value; cout << “Enter value: ”; cin >> value; return value; } // end getValue

Page 18: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 18

Function with input and output

area diametercalcArea

Prototype: double calcArea(double);

Call: double area, diameter; area = calcArea (diameter);

Definition: double calcArea(double diameter) { double result, radius; const double PI = 3.14159; radius = diameter / 2; result = PI * radius * radius; return result; } // end calcArea

Page 19: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 19

This is a lot to remember!

• Don’t try to memorize the four combinations, learn the model!– A function may or may not require values to be

passed to the function – A function may or may not return a value– All communication of values requires

specifying the data type of the value– All functions require a prototype, call and

definition

Page 20: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 20

Create prototype from the picture

• Ask yourself the following question– “Does the function return a value?”

• If the answer is no, write void

• If the answer is yes, write the data type of the value returned

area(double)

diameter(double)

calcArea

double

Page 21: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 21

Create prototype from the picture

• Ask yourself the following question– “What is the name of the function?”

• Write the function name

area(double)

diameter(double)

calcArea

double calcArea

Page 22: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 22

Create prototype from the picture

• Ask yourself the following question– “Does the function require values to be passed

to it?”• If the answer is no, write ( );

• If the answer is yes, write the data type of the value(s) passed to the function

area(double)

diameter(double)

calcArea

double calcArea (double);

Page 23: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 23

The Call Statement

• Will always contain the function name followed by parenthesis– If function does require values to be passed to it

• function_name ();

– If function requires values to be passed• function_name (value1, value1, …. valueN);

– If function does not return a value• Statement will begin with the function name

– If function does return a value• function_name will usually be on the right hand side of an

assignment statement

Page 24: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 24

The Function Definition

• The first line of the function definition is called the “function header”

• The function header looks a lot like the prototype with two exceptions– It does NOT end in a semi-colon– If values are passed to the function, names of

identifiers MUST be placed after the data types

Page 25: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 25

Things must match

• The data_type (void for none) of the value returned must be consistent between the prototype and function header

• The function_name must be the same in the prototype, call and function header

• The number of values passed to the function, the sequence of the values, and the data_types of the values must be the same in the prototype, call and function header

Page 26: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 Jade Lindquist 26

Functions as Black Boxes

You can visualize a function as a black box. To call the function, the calling function must know:• the function’s name, • the number, order, and type of input into the function, and • the function’s return type.

Page 27: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 Jade Lindquist 27

Functions are black boxes

doubledouble

calcArea

double calcArea(double);

Page 28: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 Jade Lindquist 28

Black Box

doubledouble calcArea(double myDiameter){ double result, radius; const double PI = 3.14159; radius = myDiameter / 2; result = PI * pow(radius, 2); return result;}

double

Only the function knows the names of its formal parameters and its local variables.

Page 29: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 Jade Lindquist 29

Example

Given the function’s signature and comments, a program should know how to call the function.

// This function calculates and returns the // mowing time given three inputs: area of the house, // area of the lot, and the mowing rate.

// double getMowingTime(double, double, double);

Page 30: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 Jade Lindquist 30

My program sees the function getMowingTime as a black box that requires 3 doubles (area of the house, area of the lot, mowing rate) as input and that returns a double (mowingTime) as output.

double, double, double

double

getMowingTime

double getMowingTime(double, double, double);

Page 31: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 Jade Lindquist 31

From the comments and function prototype, my program knows how to call the getMowingTime function.

// This function calculates and returns the // mowing time given three inputs: area of the house, // area of the lot, and the mowing rate.

double getMowingTime(double, double, double); // function prototype

int main(){

double houseArea, lotArea,mowingRate,mowingTime;

… code not shown -- variables get their values from the user.

mowingTime = getMowingTime(houseArea, lotArea, mowingRate);…}

Page 32: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 Jade Lindquist 32

Example, cont.The calling function’s arguments are assigned

to the called function’s parameters:mowingTime = getMowingTime(houseArea, lotArea, mowingRate);

double getMowingTime(double myHouseArea, double myLotArea, double myMowingRate)

Page 33: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 33

Debug Techniques

• Always compile your code as soon as possible– Create function stubs (empty functions)

• Compile often and frequently– Compile after changing two or three lines of code

• Code the output function(s) FIRST – They can be used to display the values from the

program as they are input or calculated

Page 34: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 34

String Class

• The 1998 ANSI standard added a string class to C++

• To use this class you need to add #include <string>

• String is technically a class, C++ treats classes as “user defined data type”

• Each value declared as “type string” is technically called an object

Page 35: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 35

How to input a string value

• Values of type string (that do not contain blanks) may be read using cin

• Values of type string (containing blanks) may be read using getline (we’ll cover this again later)

• Values of type string may be added to each other– Adding one string to another string makes a longer

string – this is called concatenation

Page 36: Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 36

More on the string class

• Objects of class string may be passed to, or returned from, a function– Follow the same pattern for prototypes, calls

and definitions as if it were like any other data type

– Just use the word “string” like a data type