Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315."— Presentation transcript:

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

2 Copyright © 2002 W. A. Tucker2 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?”

3 Copyright © 2002 W. A. Tucker3 Algorithm Get required input from user Calculate weight of the batch of washers Display the weight of the batch of washers

4 Copyright © 2002 W. A. Tucker4 Washer Problem Weight 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 * radius 2 radius = diameter / 2 Required Input: quantity outer diameter hole diameter thickness density

5 Copyright © 2002 W. A. Tucker5 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

6 Copyright © 2002 W. A. Tucker6 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)

7 Copyright © 2002 W. A. Tucker7 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)

8 Copyright © 2002 W. A. Tucker8 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

9 Copyright © 2002 W. A. Tucker9 Built in Math Functions There are many mathematical functions that are provided with the compiler #include 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

10 Copyright © 2002 W. A. Tucker10 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

11 Copyright © 2002 W. A. Tucker11 Washer Problem Flowchart start Get quantity Get outer diam Get hole diam Get thickness Get density calcArea outer circle calcArea inner circle Calculate rim area rim vol, rim weight weight of batch Display weight of batch stop

12 Copyright © 2002 W. A. Tucker12 Flowchart for calcArea function start calcArea radius = diameter / 2 area = PI * radius * radius return area Input parameter diameter Processing value PI = 3.14159

13 Copyright © 2002 W. A. Tucker13 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

14 Copyright © 2002 W. A. Tucker14 Function Communication No Input No Output InputNo Output No InputOutput Input

15 Copyright © 2002 W. A. Tucker15 Function with no input or output No Input No OutputprintLine Prototype: void printLine(); Call: printLine(); Definition: void printLine() { cout << “----------------------------”; cout << endl; } // end printLine

16 Copyright © 2002 W. A. Tucker16 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

17 Copyright © 2002 W. A. Tucker17 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

18 Copyright © 2002 W. A. Tucker18 Function with input and output areadiametercalcArea 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

19 Copyright © 2002 W. A. Tucker19 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

20 Copyright © 2002 W. A. Tucker20 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

21 Copyright © 2002 W. A. Tucker21 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

22 Copyright © 2002 W. A. Tucker22 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);

23 Copyright © 2002 W. A. Tucker23 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

24 Copyright © 2002 W. A. Tucker24 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

25 Copyright © 2002 W. A. Tucker25 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

26 Copyright © 2002 Jade Lindquist26 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.

27 Copyright © 2002 Jade Lindquist27 Functions are black boxes double calcArea double calcArea(double);

28 Copyright © 2002 Jade Lindquist28 Black Box double double 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.

29 Copyright © 2002 Jade Lindquist29 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);

30 Copyright © 2002 Jade Lindquist30 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);

31 Copyright © 2002 Jade Lindquist31 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); … }

32 Copyright © 2002 Jade Lindquist32 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)

33 Copyright © 2002 W. A. Tucker33 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

34 Copyright © 2002 W. A. Tucker34 String Class The 1998 ANSI standard added a string class to C++ To use this class you need to add #include String is technically a class, C++ treats classes as “user defined data type” Each value declared as “type string” is technically called an object

35 Copyright © 2002 W. A. Tucker35 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

36 Copyright © 2002 W. A. Tucker36 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


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

Similar presentations


Ads by Google