Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.

Slides:



Advertisements
Similar presentations
Quality of a Class Abstraction: Coupling & Cohesion Michael L. Collard, Ph.D. Department of Computer Science Kent State University.
Advertisements

Chapter 3: Modularization
Programming Logic and Design Eighth Edition
Communication between modules, cohesion and coupling
Programming Logic and Design Fourth Edition, Introductory
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Programming Logic and Design, Third Edition Comprehensive
Programming Logic and Design Fourth Edition, Introductory
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Module: Definition ● A logical collection of related program entities ● Not necessarily a physical concept, e.g., file, function, class, package ● Often.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Using C++ Functions Object-Oriented Programming Using C++ Second Edition 4.
Modules, Hierarchy Charts, and Documentation
Chapter 12: Advanced Modularization Techniques
Chapter 13: Object-Oriented Programming
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 7 Software Engineering Introduction to CS 1 st Semester, 2015 Sanghyun Park.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
1 Using C++ Functions Object-Oriented Programming Using C++ Second Edition 4.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
Chapter 9: Value-Returning Functions
Programming Logic and Design Seventh Edition
Chapter 7: User-Defined Functions II
Coupling and Cohesion 1.
Chapter 3: Using Methods, Classes, and Objects
C++ for Engineers and Scientists Second Edition
Starting Out with Programming Logic & Design
Chapter 6 Methods: A Deeper Look
Object-Oriented Programming Using C++ Second Edition
Introduction to Classes and Objects
Programming Logic and Design Fourth Edition, Comprehensive
Software Design Lecture : 9.
Topics Introduction to Functions Defining and Calling a Function
Starting Out with Programming Logic & Design
Communication between modules, cohesion and coupling
Standard Version of Starting Out with C++, 4th Edition
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods

Programming Logic and Design, Fifth Edition, Comprehensive2 Objectives Review how to use a simple method with local variables and constants Create a method that requires a single parameter Create a method that requires multiple parameters Create a method that returns a value Pass an array to a method

Programming Logic and Design, Fifth Edition, Comprehensive3 Objectives (continued) Overload methods Learn how to avoid ambiguous methods Use prewritten, built-in methods Create an IPO chart Learn to reduce coupling and increase cohesion

Programming Logic and Design, Fifth Edition, Comprehensive4 Review of Simple Methods Method: programming module that contains a series of statements to perform a task –Can invoke a method from another program or method –Program can contain unlimited number of methods –Method can be called unlimited number of times Method must include: –Header –Body –Return statement

Programming Logic and Design, Fifth Edition, Comprehensive5 Review of Simple Methods (continued) Simple methods require no arguments and send no return values Variables are in scope (local) to the method in which they are declared Two or more methods may access the same data –Pass data from one method to another

Programming Logic and Design, Fifth Edition, Comprehensive6 Figure 7-1 A program that calculates the user’s weight on the moon

Programming Logic and Design, Fifth Edition, Comprehensive7 Figure 7-2 Output of moon weight calculator program in Figure 7-1

Programming Logic and Design, Fifth Edition, Comprehensive8 Creating Methods That Require a Single Parameter Some methods require outside information Parameters: communications received by a method Argument: value in parentheses when calling a method Implementation hiding: encapsulation of method details –Program need not know how method works internally Interface to the method is the only part of the method the client sees

Programming Logic and Design, Fifth Edition, Comprehensive9 Figure 7-3 Moon weight program that passes an argument to a method

Programming Logic and Design, Fifth Edition, Comprehensive10 Figure 7-3 Moon weight program that passes an argument to a method (continued)

Programming Logic and Design, Fifth Edition, Comprehensive11 Figure 7-4 Typical execution of moon weight program in Figure 7-3

Programming Logic and Design, Fifth Edition, Comprehensive12 Creating Methods That Require a Single Parameter (continued) Method declaration must include –Type of the parameter –Local name for the parameter Parameters in parentheses hold values that are “dropped in” to the method Variable declared in method header is a local variable –Goes out of scope when method ends Variables passed to a method are passed by value –Copy of the value sent to the method Stored in a new memory location

Programming Logic and Design, Fifth Edition, Comprehensive13 Figure 7-5 Alternate version of the moon weight program in which the programmer uses the same identifier for two variables

Programming Logic and Design, Fifth Edition, Comprehensive14 Figure 7-5 Alternate version of the moon weight program in which the programmer uses the same identifier for two variables (continued)

Programming Logic and Design, Fifth Edition, Comprehensive15 Creating Methods That Require Multiple Parameters Method can require more than one parameter List data types and local identifiers in method header Parameters are separated by commas in the header Any number of parameters in any order When calling a method, arguments must match in order and in type

Programming Logic and Design, Fifth Edition, Comprehensive16 Figure 7-6 A program that calls a computeTax() method that requires two parameters

Programming Logic and Design, Fifth Edition, Comprehensive17 Creating Methods That Return Values Variables declared within a method go out of scope when method ends Retain a value that exists in a method, return the value from the method Methods that return a value must have a return type Return type can be any data type Void method returns nothing

Programming Logic and Design, Fifth Edition, Comprehensive18 Figure 7-7 A payroll program that calls a method that returns a value

Programming Logic and Design, Fifth Edition, Comprehensive19 Figure 7-8 A program that uses a method’s returned value without storing it

Programming Logic and Design, Fifth Edition, Comprehensive20 Creating Methods That Return Values (continued) Method’s return type is known as the method’s type Value in a return statement sent from called method to calling method Method’s type must match return type Most programming languages allow multiple return statements

Programming Logic and Design, Fifth Edition, Comprehensive21 Figure 7-9 Unrecommended approach to returning one of several values

Programming Logic and Design, Fifth Edition, Comprehensive22 Figure 7-10 Recommended approach to returning one of several values

Programming Logic and Design, Fifth Edition, Comprehensive23 Passing an Array to a Method Array: list of elements –Single element used in same manner as single variable of the same type Single array element passed by value –Copy of array element passed to method Entire array passed by reference Method receives memory address of the array –Accesses values in array elements Changes to array elements within method are permanent

Programming Logic and Design, Fifth Edition, Comprehensive24 Figure 7-11 PassArrayElement program

Programming Logic and Design, Fifth Edition, Comprehensive25 Figure 7-11 PassArrayElement program (continued)

Programming Logic and Design, Fifth Edition, Comprehensive26 Figure 7-12 Output of PassArrayElement program

Programming Logic and Design, Fifth Edition, Comprehensive27 Figure 7-13 PassEntireArray program

Programming Logic and Design, Fifth Edition, Comprehensive28 Figure 7-13 PassEntireArray program (continued)

Programming Logic and Design, Fifth Edition, Comprehensive29 Figure 7-13 PassEntireArray program (continued)

Programming Logic and Design, Fifth Edition, Comprehensive30 Figure 7-14 Output of PassEntireArray program

Programming Logic and Design, Fifth Edition, Comprehensive31 Overloading Methods Overloading: supplying diverse meanings for a single identifier Examples: –Overloaded English words: “break a window” “break bread” “take a break” “break the bank” –Operators such as + Overload a method: write multiple methods with same name, different parameter lists

Programming Logic and Design, Fifth Edition, Comprehensive32 Figure 7-15 The printBill() method with a numeric parameter

Programming Logic and Design, Fifth Edition, Comprehensive33 Figure 7-16 The printBill() method with two numeric parameters

Programming Logic and Design, Fifth Edition, Comprehensive34 Figure 7-17 The printBill() method with a numeric parameter and a string parameter

Programming Logic and Design, Fifth Edition, Comprehensive35 Figure 7-18 The printBill() method with two numeric parameters and a string parameter

Programming Logic and Design, Fifth Edition, Comprehensive36 Overloading Methods (continued) More natural for methods’ clients to use single method name for similar tasks Overloading never required –Could create multiple methods with unique identifiers Overloading methods does not reduce the work of programming Advantage: one appropriate name for all related tasks

Programming Logic and Design, Fifth Edition, Comprehensive37 Avoiding Ambiguous Methods When overloading a method, you risk creating ambiguous methods If compiler cannot determine which method to use, it generates an error message Methods may be valid individually, but not valid when together in the same program Provide different parameter lists for methods with the same name Illegal methods have identical names and identical parameter lists

Programming Logic and Design, Fifth Edition, Comprehensive38 Figure 7-19 Program that contains ambiguous method call

Programming Logic and Design, Fifth Edition, Comprehensive39 Figure 7-19 Program that contains ambiguous method call (continued)

Programming Logic and Design, Fifth Edition, Comprehensive40 Using Prewritten, Built-in Methods All modern programming languages contain prewritten methods Commonly available built-in methods include mathematical functions Use language documentation to determine what built- in methods are available To use a built-in method, you need: –What the method does in general –Method’s name –Method’s parameters –Method’s return type

Programming Logic and Design, Fifth Edition, Comprehensive41 Using an IPO Chart IPO chart: –Identifies and categorizes each item within a module as pertaining to input, processing, or output –Provides overview of processing steps in module Figure 7-20 IPO chart for the module that finds the smallest of three numeric values

Programming Logic and Design, Fifth Edition, Comprehensive42 Figure 7-21 Flowchart and pseudocode of findSmallest() module

Programming Logic and Design, Fifth Edition, Comprehensive43 Reducing Coupling and Increasing Cohesion Deciding how to break up a program into modules can be difficult Placing too many or too few instructions in a single module: –Decreases code readability –Reduces reliability Two rules for organizing program into modules –Reduce coupling –Increase cohesion

Programming Logic and Design, Fifth Edition, Comprehensive44 Reducing Coupling Coupling –Measure of the strength of the connection between two program modules –Expresses the extent to which information is exchanged by subroutines Tight coupling –When modules excessively depend on each other –Makes programs more prone to errors –Many data paths to keep track of –Many chances for bad data to be passed –Many chances that one module alters data needed by another

Programming Logic and Design, Fifth Edition, Comprehensive45 Reducing Coupling (continued) Loose coupling –Occurs when modules do not depend on other modules Reduce coupling as much as possible to improve: –Ease of writing the program –Program maintainability –Module reuse Evaluating the amount of coupling –Look at the intimacy between modules and the number of parameters passed between them

Programming Logic and Design, Fifth Edition, Comprehensive46 Reducing Coupling (continued) Tight coupling –Least intimate situation: modules have access to the same globally defined variables Loose coupling –Most intimate way to share data is pass a copy of variables between modules –Loosest (best) subroutines pass single arguments only

Programming Logic and Design, Fifth Edition, Comprehensive47 Increasing Cohesion Cohesion –Measure of how much a module’s statements serve to accomplish the module’s purpose Highly cohesive modules more reliable Functional cohesion –Occurs when all operations in a module contribute to the performance of a single task –Highest level of cohesion

Programming Logic and Design, Fifth Edition, Comprehensive48 Increasing Cohesion (continued) Procedural cohesion –Series of steps that use unrelated data Dispatcher module –If module only has procedural cohesion –Calls other modules to perform tasks Logical cohesion –Member module performs tasks based on a logical decision

Programming Logic and Design, Fifth Edition, Comprehensive49 Summary Method contains statements to carry out a task Variables and constants declared in a method only have scope in that method Calling method sends argument to called method –Multiple arguments: comma-separated list in header Method has a return type or void Single array element passed by value –Entire array passed by reference

Programming Logic and Design, Fifth Edition, Comprehensive50 Summary (continued) Overloaded method: same name, different parameter lists Ambiguous methods: same name, same parameter lists All modern programming languages have prewritten methods IPO chart: identifies and categorizes each item pertaining to input, processing, or output Strive to achieve loose coupling and high cohesion