Download presentation
Presentation is loading. Please wait.
Published byLucy Gray Modified over 8 years ago
1
Computer Programming 12 Lesson 4 - Computer Programming Structure By Dan Lunney
2
Tips to Structuring a Program Use modules – break program into parts each with a specific function Use modules – break program into parts each with a specific function Use logical structures to ensure program flows smoothly Use logical structures to ensure program flows smoothly Sequential structure Sequential structure Decision structure Decision structure Loop structure Loop structure Case structure Case structure
3
Tips to Structuring a Program (cont) Eliminate the rewriting of identical processes by using modules Eliminate the rewriting of identical processes by using modules Improve program readability by using the 4 logical structures, proper variable names, internal documentation, and proper indentation Improve program readability by using the 4 logical structures, proper variable names, internal documentation, and proper indentation
4
Cohesion and Coupling Modules in a program should be functionally independent, that is, they should be designed to complete one task Modules in a program should be functionally independent, that is, they should be designed to complete one task This is known as cohesion This is known as cohesion
5
Cohesion and Coupling Coupling refers to the way modules are connected through the transfer a data Coupling refers to the way modules are connected through the transfer a data Three coupling techniques are parameters, modules, global variables Three coupling techniques are parameters, modules, global variables
6
Types of Modules Control modules Control modules Init modules Init modules Process Data modules Process Data modules Calculation modules Calculation modules Print modules Print modules Read and data validation modules Read and data validation modules Wrap-up modules Wrap-up modules
7
Control Module Main module Main module Shows the overall flow of data Shows the overall flow of data All other modules are subordinate to this module All other modules are subordinate to this module
8
Init Modules Also called Initialization module Also called Initialization module Processes instructions that are usually only executed once Processes instructions that are usually only executed once Examples – opening files, setting variables opening values, or printing report headings Examples – opening files, setting variables opening values, or printing report headings
9
Process Data Modules Calculation modules – includes arithmetic, sums, counting, ordering, etc. can be both numerical and text data Calculation modules – includes arithmetic, sums, counting, ordering, etc. can be both numerical and text data Print modules – send output to the screen, printer, data file, etc Print modules – send output to the screen, printer, data file, etc Read / Data Validation modules – get input from the user and validate the info (check to make sure the data is accurate) Read / Data Validation modules – get input from the user and validate the info (check to make sure the data is accurate)
10
Wrap-up Modules Process all instructions that are only executed once at the end of a program Process all instructions that are only executed once at the end of a program Examples include – closing data files, printing summary report information such as totals and averages Examples include – closing data files, printing summary report information such as totals and averages
11
Scope of Local and global Variables Scope refers to the location and use of a variable Scope refers to the location and use of a variable Variables can have 2 scopes Variables can have 2 scopes Global Global Local Local
12
Global Variables Defined outside of the modules Defined outside of the modules Can be seen and use by all modules Can be seen and use by all modules Only variables used by several modules should be declared as global Only variables used by several modules should be declared as global
13
Local Variables Declared within the module they are used Declared within the module they are used Other modules do not know these variables exist Other modules do not know these variables exist This avoids the worry of duplicate variables in programs written by several programmers This avoids the worry of duplicate variables in programs written by several programmers
14
Parameters Local variables, usually declared in the control module, that are passed to other modules to send the data Local variables, usually declared in the control module, that are passed to other modules to send the data Two types of parameters Two types of parameters Call-by-value parameters Call-by-value parameters Call-by-reference parameters Call-by-reference parameters
15
Call-by-Value Parameter Sends the value stored in the memory location to the module Sends the value stored in the memory location to the module
16
Call-by-Reference Parameter Sends the variable location to the module as the value is not assigned yet Sends the variable location to the module as the value is not assigned yet
17
Example Control Pay Process Read(*HOURS, *PAYRATE) Process Calc(HOURS, PAYRATE,*PAY) Process Print(PAY) End Read (*HRS, *RATE) Enter HRS, RATE Print HRS, RATE Exit Calc (HRS, RATE, *PAY) PAY = HRS*RATE Exit Print (PAY) Print PAY Exit * Indicates call- by-reference parameter
18
Data Dictionary ItemName Data type ModuleScope Pseudon yms Error Checks Hours worked HOURS Num - real ControlLocalHRSNone Hours worked HRS Num - real Read / Calc ParamHOURSHours<0 Pay rate PAYRATE Num - real ControlLocalRATENone Pay rate RATE Num - real Read / Calc ParamPAYRATE Payrate< 4.00 Net pay PAY Num - real ControlLocalNoneNone Net pay PAY Num - real Calc / Print ParamNoneNone
19
Sample Problem – Lesson 4 See sample problem sheet See sample problem sheet See sample problem sheet See sample problem sheet
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.