chapter 12: advanced modularization techniques programming logic and design, third edition...

Click here to load reader

Download Chapter 12: Advanced Modularization Techniques Programming Logic and Design, Third Edition Comprehensive

Post on 21-Dec-2015

218 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • Chapter 12: Advanced Modularization Techniques Programming Logic and Design, Third Edition Comprehensive
  • Slide 2
  • 2 Objectives After studying Chapter 12, you should be able to: Understand local and global variables and encapsulation Pass a single value to a module Pass multiple values to a module Return a value from a module
  • Slide 3
  • Programming Logic and Design, Third Edition Comprehensive3 Objectives ( continued ) Use prewritten, built-in modules Create an IPO chart Understand the advantages of encapsulation Reduce coupling and increase cohesion in your modules
  • Slide 4
  • Programming Logic and Design, Third Edition Comprehensive4 Understanding Local and Global Variables and Encapsulation A procedural program consists of a series of steps or procedures that take place one after the other Possible to write as one long series of steps However, by now you should appreciate the benefits of modularization: breaking programs into reasonable units called modules, subroutines, functions, or methods
  • Slide 5
  • Programming Logic and Design, Third Edition Comprehensive5 Understanding Local and Global Variables and Encapsulation ( continued ) Benefits of modularization: Provides abstraction: easier to see the big picture Allows multiple programmers to work on a problem, each contributing one or more modules that later can be combined into a whole program Allows you to reuse your work you can call the same module from multiple locations within a program Allows you to identify structures more easily
  • Slide 6
  • Programming Logic and Design, Third Edition Comprehensive6 Understanding Local and Global Variables and Encapsulation ( continued ) Despite many benefits, two major drawbacks of modularization exist: Although modules allow multiple programmers to work on a problem, each programmer must know all variable names used in other modules within the program Although modules enable you to reuse your work, you cant use modules in other programs unless these programs use the same variable names
  • Slide 7
  • Programming Logic and Design, Third Edition Comprehensive7 Understanding Local and Global Variables and Encapsulation ( continued ) Global variable: available to every module in a program every module has access to the variable, can use its value, and can change its value Local variable name and value are known only to its own module declared within a module ceases to exist when the module ends
  • Slide 8
  • Programming Logic and Design, Third Edition Comprehensive8 Understanding Local and Global Variables and Encapsulation ( continued ) Within a module, a variable is said to be in scopethat is, existing and usablefrom the moment it is declared until it ceases to exist then it is out of scope
  • Slide 9
  • Programming Logic and Design, Third Edition Comprehensive9 Mainline Logic for a Program That Uses Only Local Variables
  • Slide 10
  • Programming Logic and Design, Third Edition Comprehensive10 Understanding Local and Global Variables and Encapsulation ( continued ) When you declare local variables within modules, you do so in a declare variables step When you use local variables: Programmers of other modules do not need to know your variable names Each module becomes enclosed unit, declaring all the variables it needs
  • Slide 11
  • Programming Logic and Design, Third Edition Comprehensive11 Understanding Local and Global Variables and Encapsulation ( continued ) The arithmetic drill program employs encapsulation (also know as information hiding or data hiding): Data or variables are completely contained withinand accessible only tothe module in which they are declared
  • Slide 12
  • Programming Logic and Design, Third Edition Comprehensive12 The Modified housekeeping() Module Containing userAnswer Variable
  • Slide 13
  • Programming Logic and Design, Third Edition Comprehensive13 Passing a Single Value to a Module Sometimes more than one module needs access to the same variable value Consider a new arithmetic drill program Instead of a single arithmetic problem, it is more reasonable to expect such a program to ask the user a series of problems and keep score Figure 12-6 shows a revised askQuestion() module that accesses an array to provide a series of five questions for the arithmetic drill
  • Slide 14
  • Programming Logic and Design, Third Edition Comprehensive14 The Modified askQuestion() Module That Provides Five Problems and Keeps Score
  • Slide 15
  • Programming Logic and Design, Third Edition Comprehensive15 The Modified askQuestion() Module That Provides Five Problems and Keeps Score
  • Slide 16
  • Programming Logic and Design, Third Edition Comprehensive16 Passing a Single Value to a Module ( continued ) The module shown in Figure 12-6 correctly counts and displays the number of correct answers for the user However, suppose when the user completes the arithmetic drill, you want to print the count of correct answers the percentage of correct answers one of two messages based on the users performance
  • Slide 17
  • Programming Logic and Design, Third Edition Comprehensive17 Passing a Single Value to a Module ( continued ) The solution to using a locally declared variable within another module lies in a programs ability to pass the value of a local variable from one module to the other Passing a value means that you are sending a copy of data in one module of a program to another module for use
  • Slide 18
  • Programming Logic and Design, Third Edition Comprehensive18 The Modified askQuestion() Module That Passes correctCount to a finalStatistics() Module
  • Slide 19
  • Programming Logic and Design, Third Edition Comprehensive19 The Modified askQuestion() Module That Passes correctCount to a finalStatistics() Module
  • Slide 20
  • Programming Logic and Design, Third Edition Comprehensive20 Passing a Single Value to a Module ( continued ) Figure 12-8 shows the finalStatistics() module for the program To prepare this module to receive a copy of the correctCount value, you declare a name for the passed value within parentheses in the module header, or introductory title statement of the module The passed variable named within the module header often is called a parameter or an argument
  • Slide 21
  • Programming Logic and Design, Third Edition Comprehensive21 The finalStatistics() Module That Receives correctCount Value and Calls It numRight
  • Slide 22
  • Programming Logic and Design, Third Edition Comprehensive22 Passing Multiple Values to a Module ( continued ) In the finalStatistics() module of the arithmetic drill program in Figure 12-8, numRight, percentCorrect, and cutOff all remain in scope from the point at which they are declared until the end of the module After the finalStatistics() module receives its argument, it contains everything it needs to calculate the percentage of addition problems that the user answered correctly and to determine which message to display
  • Slide 23
  • Programming Logic and Design, Third Edition Comprehensive23 Passing Multiple Values to a Module ( continued ) Figure 12-9 shows a module named quickQuit() that includes a call to a finalStatistics() module that accepts two arguments, numRight and numPossible This version of the numPossible argument makes the module more flexible The series of arguments that appears in the module header is called an argument list
  • Slide 24
  • Programming Logic and Design, Third Edition Comprehensive24 The quickQuit() Module That Passes Two Arguments to the finalStatistics() Module
  • Slide 25
  • Programming Logic and Design, Third Edition Comprehensive25 The quickQuit() Module That Passes Two Arguments to the finalStatistics() Module
  • Slide 26
  • Programming Logic and Design, Third Edition Comprehensive26 Returning a Value From a Module ( continued ) Suppose you decide to organize the arithmetic drill program from Figure 12-9 so that the finalStatistics() module still computes the users correct percentage, but the calling module handles the printing of the final statistics
  • Slide 27
  • Programming Logic and Design, Third Edition Comprehensive27 Returning a Value From a Module ( continued ) In this case, you pass the values of the count of correct questions and the total number of questions available to the finalStatistics() module as before the finalStatistics() module must return the value of the calculated correct percentage back to the calling module
  • Slide 28
  • Programming Logic and Design, Third Edition Comprehensive28 Returning a Value From a Module ( continued ) The giveQuiz() module ( in figure 12-10 ) declares an array of questions declares a parallel array holding correct answers displays each question in sequence compares the users answer to the correct answer determines whether to add 1 to a variable used to keep track of the number of correct responses
  • Slide 29
  • Programming Logic and Design, Third Edition Comprehensive29 A giveQuiz() Module That Sends Values to an Receives a Value Returned From a finalStatistics() Module
  • Slide 30
  • Programming Logic and Design, Third Edition Comprehensive30 A giveQuiz() Module That Sends Values to an