Unformatted text preview:

10/26/20081Chapter 3: MODULESStarting Out with Programming Logic & Design First Editionby Tony GaddisModifications by M. L. Malone 09/083-1Chapter Topics3.1 Introduction3.2 Defining & Calling a Module3.3 Local Variables3.4 Passing Arguments to Modules34 g g3.5 Global Variables & Global Constants3-2What is a Module A named group of statements within a program for the purpose of performing a specific task. Most programs are modular! Supports “Divide & Conquer” approach to solving problems3-3Why use modules? Simpler codeà Small modules easier to read than one large one Code reuseà Can call modules many times Better testingà Test separately & fix errors Faster developmentà Reuse common tasks Easier facilitation of teamworkà Share the workload3-4Example of Use of Modulesfrom Chap 2: Calcula ting an Employee’s Pay Use a module for each of theseà to read inputà to calculate gross payà calculate overtime payà …à to print the paycheck3-5Main Module with a Call In It1 Module main()2 Display “I have a message for you.”3 Call showMessage()4 Display “That’s all, folks!”5End ModuleThe call5The module definition:Module showMessage()Display “Hello world.”End Module3-610/26/20082Naming a Module Use descriptive nameà Ex: printResults or print_Results à Not: pR or prtRes No spaces Underscore OK No punctuation Cannot begin with a number3-7Definition of a Module Has two partsà Header starting point of the moduleà Body statements within a moduleModule name( )StatementStatementEtc.End Module3-8Calling a Module Must be “called” in order to executeFigure 3-2 The main module3-9Flowcharting with Modules Each module drawn separately.Figure 3-6Flowchart for 3-10Program 3-1Top‐down Design Main task (problem) broken down into a series of subtasks. Each subtask repeatedly examined to dt i if it b fth bk ddetermine if it can be further broken down. Each subtask is coded.3-11Hierarchy Chart Gives visual representation of the relationship between modules. Details of the program excludedFigure 3-7 A hierarchy chart3-1210/26/20083See pp 84‐88 in text for an example of a problem designed using modules3-13Local Variable Declared inside a module Cannot be accessed by statements outside the module.  Scopeà Describes the part of program in which a variable can be accessed Variables with the same scope must have different names.3-14Passing Arguments to Modules Argumentà Data that the module needs in order to complete its taskà Any piece of data that is passed into a module when the module is called.P Parameter à A variable that receives an argument that is passed into a module. Argument & its corresponding parameter must have same data type à (not required of all languages) OK to pass multiple arguments to a module3-15ExampleFigure 3-14 Two arguments passed into two parameters3-16Pass by Value VS Pass by Reference Pass by Valueà A copy of the argument’s value is passed into the module.à One‐directional communication See previous slide…3-17Pass by Value VS Pass by Reference Pass by Reference à The argument passed into a reference variable.à Two‐way communication Calling module can communicate with called module Called module can modify the value of the argument.3-1810/26/20084Example of Pass by ReferenceModule main()Declare Integer num1Declare Integer num2Declare Integer sumgCall findSum(num1,num2,sum)Display “The sum of “ ,num1, “ and “,num2, “ is “, sumEnd ModuleSee next slide for the module itself…3-19Example of Pass by ReferenceModule findSum(Integer num1,Integer num2,Integer Ref sum) Set sum = num1 + num2End Module3-20See Program 3‐9 (pg 99‐100) Note first time x, y, & z printed out they have values 99, 100, and 101 respectively. Next time they are printed out, what values d th h? Wh ?do they have? Why?3-21Global Variable Accessible to all modules. Should be avoided because…à Harder to debugHd dl i h àHarder to reuse module in other programsà They make a program hard to understand See pg 104 for a global variable declaration à Comes before the main module!à BUT DO NOT USE THEM! 3-22Global Constant A named constant that is available to every module in the program.  Safer than global variables because can’t be hdchanged Same problems as with global variables (previous slide)3-23See Exercise 3‐3, page 113:HOW MUCH INSURANCEFinancial experts advise that property owners should insure their homes for at least 80% of the amount it would cost to replace them. Design a MODULARprogram that asks the Design a MODULARprogram that asks the user to enter the replacement cost of the building and then displays the amount of insurance he or she should buy for that property.3-2410/26/20085How to start? Define variables by name & type Then high level (main) module in ENGLISH!à Ex: Get the replacement costCalculate the insurance amountCalculate the insurance amountDisplay the minimum amt of insurance Determine arguments/parametersà That is, how data will flow through the program Stick with the English algorithm…then translate to pseudocode3-25Read/Study Chapter


View Full Document

CHESAPEAKE CSC 103 - Chapter 3: MODULES

Download Chapter 3: MODULES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 3: MODULES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 3: MODULES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?