41
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Embed Size (px)

Citation preview

Page 1: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Lecture 9m: Top-Down Design with Functions

COS120 Software Development Using C++ AUBG, COS dept

Page 2: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

2

Exercise 8.1

Define a function and Build a program:

to compute the area of a circle

double FindArea(double rad);

Page 3: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

3

Exercise 8.2

Define a function and Build a program:

to compute the circumference of a circle

double FindCircum(double rad);

Page 4: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

4

Exercise 8.3

Define a function and Build a program:

to compute the area of a square

int FindSqArea(int side);

Page 5: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

5

Exercise 8.4

Define a function and Build a program:

to compute the perimeter of a square

int FindSqPerim(int side);

Page 6: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

6

Exercise 8.5

Define a function and Build a program:

to solve the Miles-to-Kilometers conversion problem

double ConvMilesToKms(double kms);

Page 7: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

7

Library functionsList of some Mathematical Library Functions

(#include <cmath>, data type double):Function Purpose (return value)sqrt(x) Square root (x>=0)sin(x) Sine of angle x (in radians)pow(x,y) xy (power operator)cos(x) Cosine of angle x (in radians)exp(x) ex (e=2.71828…)tan(x) Tangent of angle x (in radians)log(x) Natural logarithm (x>0)ceil(x) Smallest integral not < than xlog10(x) Base-10 logarithm (x>0)floor(x) Largest integral not > than x

Page 8: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

8

Library functionsList of some Character Library Functions (#include <cctype>)

Function Purpose (return value)char tolower(char c)

Returns lowercase letter if c is uppercase. Otherwise, returns c.char toupper(char c)

Returns uppercase letter if c is lowercase. Otherwise, returns c.bool isalpha(char c)

Returns true if c is a letter (‘a’ … ‘Z’), otherwise false.bool isalnum(char c)

Returns true if c is a letter or a digit, otherwise false.bool isdigit(char c)

Returns true if c is a digit (‘0’ … ‘9’), otherwise false.bool isxdigit(char c)

Returns true if c is a hex digit (‘0’…’9’, ‘a’… ‘f’, ‘A’…’F’).bool isspace(char c)

Returns true if c is a space or tab(‘\t’) or new line (‘\n’).

Page 9: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

9

Before lecture end

Lecture:

Top-Down Design using Functions

More to read:

Friedman/Koffman, Chapter 03

Page 10: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3: Top-Down Design with Functions and Classes

Problem Solving,

Abstraction, and Design using C++ 5e

by Frank L. Friedman and Elliot B. Koffman

Page 11: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11

Functions with Input Arguments

• Functions used like building blocks

• Build systems one function at a time– E.g. stereo components

• Use function arguments to carry information into function subprogram (input arguments) or to return multiple results (output arguments)

Page 12: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12

Functions with Input Arguments

• Arguments make functions versatile

• E.g.:

rimArea = findArea(edgeRadius) -

findArea(holeRadius);

Page 13: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13

void Functions with Input Arguments

• Give data to function to use

• Don’t expect function to return any result(s)

• Call format:

fname (actual-argument-list);

• E.g.:

drawCircleChar(‘*’);

Page 14: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14

drawCircleChar(‘*’);

void drawCircle(char symbol)

{

cout << “ “ << symbol << endl;

cout << “ “ << symbol << “ “ << symbol << endl;

cout << “ “ << symbol << “ “ << symbol << endl;

} // end drawCircle

‘*’symbol

Page 15: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15

Functions with Arguments and a Single Result

• Functions that return a result must have a return statement:

Form: return expression;

Example: return x * y;

Page 16: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 16

#include <iostream>#include <cmath>using namespace std;const float PI = 3.14159;float findCircum(float);float findArea(float);int main( ){ float radius = 10.0; float circum; float area; circum = findCircum(radius); area = findArea(radius); cout << “Area is “ << area << endl; cout << “Circumference is “ << circum << endl; return 0;}

Page 17: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17

// Computes the circumference of a circle with radius r

// Pre: r is defined and is > 0.

// PI is a constant.

// Post: returns circumference

float findCircum(float r)

{

return (2.0 * PI * r);

}

// Computes the area of a circle with radius r

// Pre: r is defined and is > 0.

// PI is a constant.

// Post: returns area

float findArea(float r)

{

return (PI * pow(r,2));

}

Figure 3.12 Functions findCircum and findArea

Page 18: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18

circum = findCircum(radius);

float findCircum(float r){ return (2.0 * PI * r);}

10

radius

10

r

62.8318

call findCircum62.8318

circum

Page 19: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19

Function Definition (Input Arguments with One Result)

• Syntax:

// function interface comment

ftype fname(formal-parameter-declaration-list)

{

local variable declarations

executable statements

}

Page 20: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 20

Function Definition (Input Arguments with One Result)

• Example:// Finds the cube of its argument.// Pre: n is defined.int cube(int n){

return (n * n * n);}

Page 21: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 21

Function Prototype (With Parameters)

• Form:

ftype fname(formal-parameter-type-list);

• Example:

int cube(int);

Page 22: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 22

Function Interface Comments

• Preconditions– conditions that should be true before function is

called– // Pre: r is defined

• Postconditions– conditions that will be true when function

completes execution– // Post: Returns circumference

Page 23: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 23

Problem Inputs vs. Input Parameters

• Problem inputs– variables that receive data from program user– through execution of input statement

• Input parameters– receive data through execution of function call

statement– must be defined before function is called

Page 24: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 24

Listing 3.14: Testing function testScale.cpp

// File testScale.cpp

// Tests function scale.

#include <iostream>

#include <cmath>

using namespace std;

// Function prototype

float scale(float, int);

Page 25: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 25

int main(){ float num1; int num2;

// Get values for num1 and num2 cout << "Enter a real number: "; cin >> num1; cout << "Enter an integer: "; cin >> num2; // Call scale and display result. cout << "Result of call to function scale is " << scale(num1, num2) << endl; return 0;}

Listing 3.14: Testing function testScale.cpp (continued)

Page 26: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 26

// Multiplies its first argument by the power of 10

// specified by its second argument.

// Pre: x and n are defined and library cmath is

// included

float scale(float x, int n)

{

float scaleFactor; // local variable

scaleFactor = pow(10, n);

return (x * scaleFactor);

}

Listing 3.14: Testing function testScale.cpp (continued)

Page 27: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 27

float scale(float x, int n){ float scaleFactor; // local variable scaleFactor = pow(10, n); return (x * scaleFactor);}

cout << "Result of call to function scale is " << scale(num1, num2) << endl;

.

.

.

Formal parameters

Information flow

Actual arguments

Listing 3.14: Testing function testScale.cpp (continued)

Page 28: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 28

Argument/Parameter List Correspondence• Must have same number of actual

arguments and formal parameters• Order of arguments in the lists determines

correspondence• Each actual argument must be of a data type

that is compatible to the corresponding formal parameter

• The names of the actual arguments do not need to correspond to the formal parameters

Page 29: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 29

Function Data Area

• Each time function is executed– an area of memory is allocated for storage of

the function’s data (formal parameters and local variables)

– it is created empty with each call to the function

• When the function terminates– the data area is lost

Page 30: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 30

Data Areas After Call scale(num1, num2);

Function main Data

Area

Function Scale Data

Areanum1

num2

x

n

scaleFactor

2.5

-2

2.5

-2

?

Page 31: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 31

Testing Functions Using Drivers

• Any function can be tested independently

• Test driver – defines values for the function’s arguments– calls the function– displays the value returned for verification

Page 32: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 32

Scope of Names• Scope - where a particular meaning of a

name is visible or can be referenced• Local - can be referred to only within the

one function– applies to

• formal argument names• constants and variables declared within the function

• Global - can be referred to within all functions– useful for constants– must be used with care

Page 33: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 33

Listing 3.15 Outline of program for studying scope of names

Page 34: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 34

3.2 Library Functions

• Goals of software engineering– reliable code– accomplished by code reuse

• C++ promotes code reuse with predefined classes and functions in the standard library

Page 35: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 35

C++ cmath Library

• Typical mathematical functions

e.g. sqrt, sin, cos, log

• Function use in an assignment statement

y = sqrt(x);

Function name

Function argument

Function call

Page 36: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 36

Example: sqrt Function

Square root function

Function sqrt as a “black box”

X is 16.0 Result is 4.0

Page 37: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 37

Listing 3.5 Illustration of the use of the C++ sqrt function

Page 38: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 38

Listing 3.5 Illustration of the use of the C++ sqrt function (continued)

Page 39: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 39

Table 3.1 Some Mathematical Library Functions

Page 40: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 40

Table 3.1 Some Mathematical Library Functions (continued)

Page 41: Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept

41

Thank You

For

Your Attention!