21
Simple Functions Simple Functions Writing Reuseable Formulas Writing Reuseable Formulas

Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Embed Size (px)

DESCRIPTION

Problem Generalization Using OCD, design and implement a program that computes the area and circumference of an ellipse. a a bb

Citation preview

Page 1: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Simple FunctionsSimple FunctionsWriting Reuseable FormulasWriting Reuseable Formulas

Page 2: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

ProblemProblemUsing OCD, design and implement a program Using OCD, design and implement a program

that computes the area and circumference that computes the area and circumference of an Australian Rules Football field, which of an Australian Rules Football field, which is an ellipse that is (ideally) 165m x 135m.is an ellipse that is (ideally) 165m x 135m.

135165

Page 3: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Problem GeneralizationProblem GeneralizationUsing OCD, design and implement a Using OCD, design and implement a

program that computes the area and program that computes the area and circumference of an circumference of an ellipseellipse..

2222 bancecircumfere

abarea a

a

bb

Page 4: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

BehaviorBehaviorOur program should display on the Our program should display on the

screen its purpose and prompt the screen its purpose and prompt the user for the length and width of the user for the length and width of the ellipse. It should then read the length ellipse. It should then read the length and width from the keyboard. It and width from the keyboard. It should then compute and display the should then compute and display the ellipse’s area and circumference, along ellipse’s area and circumference, along with a descriptive label.with a descriptive label.

Page 5: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

ObjectsObjectsDescription Type Kind NameDescription Type Kind Name

purpose string constant --prompt string constant --length double variable lengthwidth double variable width

circumference double variable circumferencearea double variable area

screen ostream varying cout

keyboard istream varying cin

double constant PIa double variable halfLengthb double variable halfWidth

Page 6: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

OperationsOperationsDescription Predefined? Library? NameDescription Predefined? Library? Namedisplay strings yes iostream <<read doubles yes iostream >>compute circumf. no -- --- multiply doubles yes built-in *

compute area no -- --- multiply doubles yes built-in *display doubles yes iostream <<

- add doubles yes built-in +- divide doubles yes built-in /- exponentiation yes cmath pow()- square root yes cmath sqrt()

Page 7: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

AlgorithmAlgorithm0. Display via cout the purpose of the program plus a 0. Display via cout the purpose of the program plus a

prompt for the length and width of an ellipse.prompt for the length and width of an ellipse.1. Read 1. Read lengthlength, , widthwidth from from cincin..2. Compute 2. Compute halfLengthhalfLength = = lengthlength/2.0; /2.0; halfWidthhalfWidth = =

widthwidth/2.0./2.0.3. Compute 3. Compute areaarea = = PI * halfLength * halfWidthPI * halfLength * halfWidth..4. Compute 4. Compute circumferencecircumference = 2.0 * PI * = 2.0 * PI *

sqrt((sqrt((halfLengthhalfLength22 + + halfWidth halfWidth22)/2.0). )/2.0). 5. Display 5. Display areaarea and and circumferencecircumference with descriptive with descriptive

labels.labels.

Page 8: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

CodingCoding#include <iostream> // cin, cout, <<, >>, ...#include <cmath> // sqrt(), pow(), ...using namespace std;int main(){ const double PI = 3.14159; cout << "\nTo compute the area and circumference" << "\n of an ellipse, enter its length and width: "; double length, width; cin >> length >> width; double halfLength = length/2.0, halfWidth = width/2.0; double area = PI * halfLength * halfWidth; double circumference = 2.0 * PI * sqrt((pow(halfLength, 2.0) + pow(halfWidth, 2.0))/2.0);

cout << "\nThe area is " << area << "\n and the circumference is " << circumference << endl;}

Page 9: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

TestingTesting

To compute the area and circumference

of an ellipse, enter its length and width: 165 135

The area is 17494.7

and the circumference is 473.589

Page 10: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

ProblemProblemWe’ve done a fair amount of work in We’ve done a fair amount of work in

creating these formulas, but have no creating these formulas, but have no way to directly reuse that work if we way to directly reuse that work if we ever need to use these ellipse-related ever need to use these ellipse-related formulas again...formulas again...

Solution: Rewrite those formulas as Solution: Rewrite those formulas as functionsfunctions..

Page 11: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Using Functions (i)Using Functions (i)#include <iostream> // cin, cout, <<, >>, ...#include <cmath> // sqrt(), pow(), ...using namespace std;

double EllipseArea(double length, double width);double EllipseCircumference(double length, double width);

int main(){ cout << "\nTo compute the area and circumference" << "\n of an ellipse, enter its length and width: "; double length, width; cin >> length >> width;

double area = EllipseArea(length, width); double circumference = EllipseCircumference(length,width);

cout << "\nThe area is " << area << "\n and the circumference is " << circumference << endl;}

Page 12: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Using Functions (ii)Using Functions (ii)const PI = 3.14159;

double EllipseArea(double length, double width);{ double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth;}

double EllipseCircumference(double length, double width);{ double halfLength = length/2.0, halfWidth = width/2.0; return 2.0 * PI * sqrt((pow(halfLength, 2.0) + pow(halfWidth, 2.0))/2.0);}

Page 13: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Function PrototypesFunction PrototypesA function prototype acts as a A function prototype acts as a

declaration of the function, allowing it declaration of the function, allowing it to be called.to be called.

A function prototype must precede any A function prototype must precede any call or definition of a function, or a call or definition of a function, or a compiler errorcompiler error will occur. will occur.

Pattern:Pattern:ReturnTypeReturnType NameName ( (ParameterDeclarationsParameterDeclarations));;

Page 14: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Example PrototypesExample Prototypes#include <iostream> // cin, cout, <<, >>, ...#include <cmath> // sqrt(), pow(), ...using namespace std;

double EllipseArea(double length, double width);double EllipseCircumference(double length, double width);

int main(){ cout << "\nTo compute the area and circumference" << "\n of an ellipse, enter its length and width: "; double length, width; cin >> length >> width; double area = EllipseArea(length, width); double circumference = EllipseCircumference(length,width);

cout << "\nThe area is " << area << "\n and the circumference is " << circumference << endl;}

Page 15: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Function DefinitionsFunction DefinitionsA function definition contains statements A function definition contains statements

that dictate its behavior when it is called.that dictate its behavior when it is called.A function must be defined in order to be A function must be defined in order to be

called, or else a called, or else a linker errorlinker error will occur. will occur.Pattern:Pattern: ReturnTypeReturnType NameName ( (ParameterDeclarationsParameterDeclarations) )

{ { StatementListStatementList } }

Page 16: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Example DefinitionsExample Definitionsconst PI = 3.14159;

double EllipseArea(double length, double width){ double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth;}

double EllipseCircumference(double length, double width){ double halfLength = length/2.0, halfWidth = width/2.0; return 2.0 * PI * sqrt((pow(halfLength, 2.0) + pow(halfWidth, 2.0))/2.0);}

Page 17: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

ParametersParametersParameters are function variables for Parameters are function variables for

which the caller can specify values.which the caller can specify values.Parameters are defined between the Parameters are defined between the

parentheses of a function’s definition.parentheses of a function’s definition.

double EllipseArea(double length, double width){ double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth;}

Page 18: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

ArgumentsArgumentsWhen a function is called, its caller can When a function is called, its caller can

pass it values called pass it values called argumentsarguments which which are stored in the function’s parameters.are stored in the function’s parameters.double area = EllipseArea(165, 135);

double EllipseArea(double length, double width){ double halfLength = length/2.0, halfWidth = width/2.0; return PI * halfLength * halfWidth;}

165 135

The function then The function then runsruns using its parameter values. using its parameter values.

Page 19: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

Program StructureProgram StructureC++ programs typically follow this C++ programs typically follow this

pattern:pattern:#include directives#include directivesFunction prototypesFunction prototypesMain functionMain functionFunction definitionsFunction definitions

Page 20: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

OCD with FunctionsOCD with Functions1. Specify the desired behavior of the program.1. Specify the desired behavior of the program.2. Identify the objects needed.2. Identify the objects needed.3. Identify the operations.3. Identify the operations.

a. If any operation is not predefined:Write a function to perform it.

4. Organize objects and operations into an 4. Organize objects and operations into an algorithm.algorithm.

Page 21: Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian

SummarySummaryFunctions provide a programmer with a Functions provide a programmer with a

means of extending C++ with new means of extending C++ with new operations that are not predefined in operations that are not predefined in the language.the language.

A function prototype must precede its A function prototype must precede its call, or a compiler error will occur.call, or a compiler error will occur.

A function definition must be provided, A function definition must be provided, or a linker error will occur. or a linker error will occur.