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

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Chapter 3 Top-Down Design with Functions. 3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF.
Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Chapter 3 Top-Down Design with Functions and Classes Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Bill Tucker Austin Community College COSC 1315
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 6: User-Defined Functions I
CSCI 161: Introduction to Programming Function
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
User Defined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 6: User-Defined Functions I
Topics Introduction to Functions Defining and Calling a Function
Functions, Part 1 of 3 Topics Using Predefined Functions
CS149D Elements of Computer Science
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 1 of 3 Topics Using Predefined Functions
Top-Down Design with Functions
Top-Down Design with Functions
Presentation transcript:

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

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?”

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

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

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

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)

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)

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

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

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

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

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 =

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

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

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

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

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

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 = ; radius = diameter / 2; result = PI * radius * radius; return result; } // end calcArea

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

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

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

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

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

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

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

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.

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

Copyright © 2002 Jade Lindquist28 Black Box double double calcArea(double myDiameter) { double result, radius; const double PI = ; 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.

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

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

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

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)

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

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

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

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