introduction to programming structure lesson 4 mcmanuscop10061
TRANSCRIPT
![Page 1: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/1.jpg)
COP1006 1
Introduction to Programming Structure
Lesson 4
McManus
![Page 2: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/2.jpg)
COP1006 2
Overview
• Pointers• Modules & Functions• Cohesion & Coupling• Local & Global Variables• Parameters• Variable Names & Data Dictionaries• Three Logic Structures
McManus
![Page 3: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/3.jpg)
COP1006 3
Pointers on Program Structure
• Use Modules– Each part should have a particular
function• Use the three logic structures
– Sequential, Decision & Iteration• Don’t reinvent the wheel
– Avoid rewriting identical processes• Use techniques to improve
readabilityMcManus
![Page 4: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/4.jpg)
COP1006 4
Modules, Procedures & Functions
The subparts to a Program
McManus
![Page 5: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/5.jpg)
COP1006 5
Modules
• “A module is a lexically contiguous sequence of program statements, bounded by boundary elements, having an aggregate identifier.” Yourdon & Constantine (1979)– A part of a larger system– Written and tested separately– Combined with other modules to form a
complete system– Used in top-down programming– Procedures & Functions
McManus
![Page 6: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/6.jpg)
COP1006 6
Procedures
• A smaller part of the main program.• 2 Advantages
– Eliminates the need to program the same thing more than once.
– Larger programs are easier to read when broken into procedures (and functions).
McManus
![Page 7: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/7.jpg)
COP1006 7
Functions
• Functions– A subprogram that acts like a
mathematical function: • given a particular set of argument values,
the function returns a unique result.• Use Return values that are associated with
the name of the function
McManus
![Page 8: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/8.jpg)
COP1006 8
Function Examples
• PascalFUNCTION doublenum(b : Integer) : Integer; BEGIN doublenum := 2 * b END;
• Visual BasicPrivate Function Doublenum(b As Integer) As Integer Doublenum = 2 * bEnd Function
• C++Int doublenum ( int b){ return 2 * b;}
McManus
![Page 9: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/9.jpg)
COP1006 9
What do they have in common?
• Each module – is an entity by itself– has a single purpose– should be easily read, modified and
maintained– Length is governed by function and
number of instructions contained within– Controls the order of processing
McManus
![Page 10: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/10.jpg)
COP1006 10
An Example of Control
McManus
![Page 11: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/11.jpg)
COP1006 11
Types of Modules/Procedures
• Control – Demonstrates
overall flow of data• Initialization &
Wrap-Up– Processes
instructions to be performed once (either at beginning or at the end of the program)
– Used typically in batch processing
• Process Data– Calculation– Print– Read and Validation
• Event– Used in OO and
some event driven programming languages
– More about these later
McManus
![Page 12: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/12.jpg)
COP1006 12
Control Modules
• Most often called “Main”• All other modules, procedures and
functions are subordinate to the control module
Sub MainCall ProcedureA(X, Y)Call ProcedureB(A, B)
End Main
McManus
![Page 13: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/13.jpg)
COP1006 13
Init Module
• Used in some languages to initialize variables or processes– Examples
• Opening files• Initializing variables• Printing report headings
Procedure BeginDim X, Y, Z As IntegersOpen Payroll file
End Procedure ‘BeginMcManus
![Page 14: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/14.jpg)
COP1006 14
Process Data Modules
• Calculation– Performs
• Arithmetic operations
• Accumulations• Sorting or Searching
Private Sub Double (X, Y)Dim Temp as IntegerTemp = X * Y
End Sub
McManus
Read and Data Validation
Reads and validates input data
• Usually separate modules
Private Sub Verify(X)If X < 0 Or X > 10 Then
lblMessage.Text = “Data Error”End If
End Sub
![Page 15: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/15.jpg)
COP1006 15
Wrap Up Module
• Used to close out processes– Examples
• Closing files• Printing reports• Returning updated data to databases
Procedure EndClose Employee fileClose Payroll file
End Procedure ‘End
McManus
![Page 16: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/16.jpg)
COP1006 16
Global or Local Variables
Scope!
McManus
![Page 17: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/17.jpg)
COP1006 17
Scope
• The area of a program where an identifier (variable) is visible
• When an identifier has multiple declarations in different modules, the most local declaration is used each time that identifier is referenced. (overloading)
• Global or “non-local” variables subject to side effects.
McManus
![Page 18: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/18.jpg)
COP1006 18
Side Effects
• Caused when the value of a global variable is changed within a procedure or function– Any effect of one module on another module
that is not a part of the explicitly defined interface between them
• Also caused when a variable name is used in globally and locally (causes overloading)
• A nasty effect that should be avoided!
McManus
![Page 19: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/19.jpg)
COP1006 19
Global Scope
• Declared within the main program• Can be referenced anywhere in the
program– Is visible and accessible everywhere
McManus
X, Y, Z
A
CB
X, Y & Z are Global to modules
A, B & C
![Page 20: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/20.jpg)
COP1006 20
Local Scope
• Declared within a module– Has no effect outside the procedure or function
in which it is declared• Can be referenced only within a procedure
or a function
McManus
X, Y, Z
A m
C pB n
Within A, Variable m is defined, but can also see X, Y, & Z
Within B, Variable n is defined , but can also see X, Y, & Z
Within C, Variable p is defined , but can also see X, Y, & Z
![Page 21: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/21.jpg)
COP1006 21
Global or Local?
Private Sub Minimum(Scully As Long, Mulder As Long)
Dim Temp As Long
If Scully < Mulder Then
temp = Scully
Scully = Mulder
Mulder = temp
End If
End Sub
McManus
Scully & Mulder are what type
of variables?
What type of variable is Temp?
![Page 22: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/22.jpg)
COP1006 22
Another Example
McManus
Scope of X, Y, Z, Procedure1
Scope of M, N, Me, X, You
program ShowScope; var X, Y, Z : Real; procedure Procedure1
(var M, N, Me : Real); var X, You : Real; begin {Procedure1} ....... end; {Procedure 1}
begin {ShowScope} Procedure1(X, Y, Z)end. {ShowScope}
![Page 23: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/23.jpg)
COP1006 23
Cohesion & Coupling
Making Modules Better!
McManus
![Page 24: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/24.jpg)
COP1006 24
Cohesion & Coupling
• Each module should– Be functionally independent– Perform one problem-related task
• Calculating IRS Withholding is one problem-related task, but may have multiple operations within the module
– When connected, use the smallest interface possible.
McManus
![Page 25: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/25.jpg)
COP1006 25
Cohesion
• The degree of interaction within a module.– Each module should perform one
functionally-related task…not necessarily one assignment statement.
– Concentration is on what goes on within the module.
McManus
Term was coined by Larry Constantine in mid-1960’s
![Page 26: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/26.jpg)
COP1006 26
Scale of Cohesion
• Stevens, Myers, Constantine, and Yourdon developed the Scale of Cohesion as a measure of the “black boxiness” of a module, and as a result, the maintainability of a module.
McManus
Type Measure Black BoxFunctional Best Black BoxInformational ** BestSequential CommunicationalProcedural Gray BoxTemporalLogicalCoincidental Worst Transparent
or White Box**Originally not part of Scale
![Page 27: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/27.jpg)
COP1006 27
Coupling
• The degree of interaction between two modules.– Interaction is the
interface, or lack thereof, between two modules.• The interface is
the parameter list.
McManus
Best (Lowest Interaction)
Worst (Highest Interaction)
Normal Data Stamp Control
Common
Content
![Page 28: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/28.jpg)
COP1006 28
Effects?
• Which pieces affect Cohesion and which affect Coupling?
McManus
Private Sub Minimum(min As Long, y As Long, z As Long)
If y < min Then
min = y
End If
If z < min Then
min = z
End If
lblSmallest.Caption = "Smallest value is " & min
End Sub
![Page 29: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/29.jpg)
COP1006 29
What is the code doing?
• What are the parameters?
McManus
Private Sub Skinner(Scully As Long, Mulder As Long)
Dim Temp As Long
If Scully < Mulder Then
Temp = Scully
Scully = Mulder
Mulder = Temp
End If
End Sub
![Page 30: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/30.jpg)
COP1006 30
Each Goal
• Cohesion’s Goal– To create a procedure that performs one
functionally-related task. • Coupling’s Goal
– To protect global data and local data from being used within a procedure without declaring it on the procedure’s header
McManus
![Page 31: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/31.jpg)
COP1006 31
Goal of Cohesion & Coupling?
• High Cohesion – Functional or Information
• Low Coupling– Data, Stamp, Control
McManus
![Page 32: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/32.jpg)
COP1006 32
Parameters
How we avoid side effects!
McManus
![Page 33: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/33.jpg)
COP1006 33
Parameters
• Are the variables that are passed into and out of modules
• Use global parameters – (to the procedure or function)
• Pass values through the use of variables
• Actual and Formal parameters• Call-by-reference & Call-by-value
McManus
![Page 34: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/34.jpg)
COP1006 34
Parameter Communication
• A measure of the quantity of data passing through a module’s interface.
• Is also a measure of the module’s coupling.
• The goal is to strive for a minimal amount of information being passed.
McManus
![Page 35: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/35.jpg)
COP1006 35
How Parameters are Used
• Input Parameter – Information passed into a procedure, but not
returned or passed out of the procedure.• Output Parameter
– Information returned to the calling program from a procedure.
• Input/Output Parameter – Information passed into a procedure, perhaps
modified, and a new value returned.
McManus
![Page 36: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/36.jpg)
COP1006 36
Parameters
• Provide the communication links between the main program and its modules.
• Make procedures and functions more versatile.– Different data can be manipulated each time
the module is called.• Come in two types:
– Actual– Formal
McManus
![Page 37: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/37.jpg)
COP1006 37
Actual & Formal Parameters
• Actual Parameters– Are substituted for the formal parameter at the
time the procedure is called.– Parameters used in the call statement
• Statements that transfer control to a procedure.– Data types must be assignment compatible
with its corresponding formal parameter– Can be a variable, constant or an expression– Can be call-by-value or call-by-reference
McManus
![Page 38: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/38.jpg)
COP1006 38
Actual & Formal Parameters
• Formal Parameters– Parameters declared in the procedure
header– Is a list of “place marker” names used in
the procedure’s declaration. – Can include the data type of the valued
parameters. – Must be a variable– Can be call-by-value or call-by-reference
McManus
![Page 39: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/39.jpg)
COP1006 39
Parameter Correspondence Rules
• Determined by position in respective parameter lists
• Lists must be the same size, although the names may differ
• Data Types of corresponding actual and formal parameters must be identical
McManus
![Page 40: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/40.jpg)
COP1006 40
Data Areas After a Call
McManus
8.0
10.0
8.0
10.0
? ?
Formal Parameters
Local Variables
Sum Average
Num1
Num2
Actual Parameters Var1
Var2
Main program data area Procedure data area
![Page 41: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/41.jpg)
COP1006 41
Valued & Variable Parameters
• By Reference (Call-by-Reference)– Passing a variable to a procedure is
called passing an argument by reference, because a variable can be modified by a procedure and returned to the calling module.
• By Value (Call-by-Value)– Passing a literal value (such as a string
in quotation marks) to a procedure is called passing an argument by value, because a value cannot be modified by a procedure.McManus
![Page 42: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/42.jpg)
COP1006 42
Call-by-Reference
• The default for parameter passing• Gives access to the contents of the
storage area where values are stored• Giving the called procedure the
ability to directly access the caller’s data
• Allowing changes in the data
McManus
![Page 43: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/43.jpg)
COP1006 43
Call-by-Value
• Protects the data being passed• Accomplished by creating a copy of
the value– without affecting the original value of
the variable• Thus…
– Called procedure is unable to change the values stored in the variable’s storage area
• Helps avoid Side Effects!McManus
![Page 44: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/44.jpg)
COP1006 44
Parameter Relationships
Call Parameters
Actual Parameters• Valued• Variable
McManus
Procedure Header Parameters
Formal ParametersValuedVariable
Parameter Interface using Global Variables
![Page 45: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/45.jpg)
COP1006 45
Another Look at Variables
Names & the Data Dictionary
McManus
![Page 46: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/46.jpg)
COP1006 46
Variable Names
• Use mnemonic terms– Use a variable name that relates the
name of the variable to its usage• Contributes to self-documenting code
– Which reduces the amount of commenting required
– Z = X * Y What is it doing (besides multiplication?)– SalesTax = SalesTaxRate * Cost (this you know)
• Examples– SalesTax, SalesRate, PayRate, Temp
McManus
![Page 47: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/47.jpg)
COP1006 47
The Data Dictionary
• Defines all of the variables used within a program
• Lists:– Names – Data type– Location defined & accessed– Test Data (or error checking)– Domain (range of possible values)
McManus
![Page 48: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/48.jpg)
COP1006 48
DD Example
Item Name(no
spaces)
Data Type
Modules Domain(Range)
Scope
Hours worked
Hours Numeric-real
GetHoursCalcGrossPay
0 n 168 GlobalGlobal
Gross Pay
GrossPay Numeric-real
CalcGrossPayCalcDeductionsCalcNetPayPrintPayChecks
0 n 1 million
GlobalGlobalGlobalGlobal
Net Pay
NetPay Numeric-real
CalcNetPayPrintPayChecks
0 n 1 million
LocalGlobal
McManus
![Page 49: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/49.jpg)
COP1006 49
The Three Logic Structures
• Sequential– One statement follows another
• Selection (Decision)– Allows choices based on the data– IfThenElse, Nested If’s, Case, Switch
• Iteration (Looping or Repetition)– Allows statements to be repeated a specified
number of times– While, Do, For, Do Until, Repeat
McManus
![Page 50: Introduction to Programming Structure Lesson 4 McManusCOP10061](https://reader035.vdocuments.net/reader035/viewer/2022081516/56649e735503460f94b73147/html5/thumbnails/50.jpg)
COP1006 50McManus
Next?