Introduction to Programming Structure - PowerPoint PPT Presentation

Introduction to programming structure l.jpg
1 / 50

  • Updated On :
  • Presentation posted in: General

Introduction to Programming Structure. Lesson 4. Overview. Guidelines Modules & Functions Cohesion & Coupling Local & Global Variables Parameters Variable Names & Data Dictionaries Three Logic Structures. Guidelines on Program Structure. Use Modules

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

Introduction to Programming Structure

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Introduction to programming structure l.jpg

Introduction to Programming Structure

Lesson 4


Overview l.jpg


  • Guidelines

  • Modules & Functions

  • Cohesion & Coupling

  • Local & Global Variables

  • Parameters

  • Variable Names & Data Dictionaries

  • Three Logic Structures


Guidelines on program structure l.jpg

Guidelines 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 readability


Cohesion coupling l.jpg

Cohesion & Coupling

Making Modules Better!


Cohesion coupling5 l.jpg

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.


Cohesion l.jpg


  • 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.

Term was coined by Larry Constantine in mid-1960’s


Scale of cohesion l.jpg

Type MeasureBlack Box

Functional BestBlack Box

Informational ** Best



ProceduralGray Box



Coincidental WorstTransparent

or White Box

**Originally not part of Scale

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.


Coupling l.jpg

Best (Lowest Interaction)







Worst (Highest Interaction)


  • The degree of interaction between two modules.

    • Interaction is the interface, or lack thereof, between two modules.

      • The interface is the parameter list.

    • Create it, use it, lose it


Effects l.jpg


  • Which pieces affect Cohesion and which affect Coupling?

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

What does this code do?


What is the code doing l.jpg

What is the code doing?

  • What are the parameters? Variables?

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

Global &Parameters

Local Variable


Each goal l.jpg

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

      • Create as close to where you’re going to use it as possible, use it, then lose it as soon as possible


Goal of cohesion coupling l.jpg

Goal of Cohesion & Coupling?

  • High Cohesion

    • Functional or Information

  • Low Coupling

    • Data, Stamp, Control


Modules procedures functions l.jpg

Modules, Procedures & Functions

The subparts to a Program


Modules l.jpg


  • “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


Procedures l.jpg


  • 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).


Functions l.jpg


  • Functions

    • A subprogram that acts like a mathematical function:

      • given a particular set of argument values, the function returns a unique result.

      • Big Difference Between Procedures & Functions:

        • Use Return values that are associated with the name of the function


Function examples l.jpg

Function Examples

  • Pascal

    FUNCTION doublenum(b : Integer) : Integer;


    doublenum := 2 * b


  • Visual Basic

    Private Function Doublenum(b As Integer) As Integer

    Doublenum = 2 * b

    End Function

  • C++

    Intdoublenum ( int b)


    return 2 * b;


All three do the same thing, just with different syntax


What do they h ave i n common l.jpg

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


An example of control l.jpg

An Example of Control

VB Example


Types of modules procedures l.jpg

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


Control modules l.jpg

Control Modules

  • Most often called “Main”

  • All other modules, procedures and functions are subordinate to the control module

    Sub Main

    Call ProcedureA(X, Y)

    Call ProcedureB(A, B)

    End Main


Init module l.jpg

Init Module

  • Used in some languages to initialize variables or processes

    • Examples

      • Opening files

      • Initializing variables

      • Printing report headings

        Procedure Begin

        Dim X, Y, Z As Integers

        Open Payroll file

        End Procedure ‘Begin

Does this violate Cohesion or Coupling?


Process data modules l.jpg

Process Data Modules

  • Calculation

    • Performs

      • Arithmetic operations

      • Accumulations

      • Sorting or Searching

        Private Sub Double (X, Y)

        Dim Temp as Integer

        Temp = X * Y

        End Sub

  • 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


Wrap up module l.jpg

Wrap Up Module

  • Used to close out processes

    • Examples

      • Closing files

      • Printing reports

      • Returning updated data to database

        Procedure LastProcedure

        Close Employee file

        Close Payroll file

        End Procedure ‘LastProcedure

Does this violate Cohesion or Coupling?


Global or local variables l.jpg

Global or Local Variables



Scope l.jpg


  • 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.


Side effects l.jpg

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!


Global scope l.jpg

X, Y, Z




Global Scope

  • Declared within the main program

  • Can be referenced anywhere in the program

    • Is visible and accessible everywhere

X, Y & Z are Global to modules A, B & C


Local scope l.jpg

X, Y, Z

A m

C p

B n

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

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


Global or local l.jpg

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

Scully & Mulderare what type of variables?

What type of variable is Temp?

VB Example


Another example l.jpg

Another Example

program ShowScope;

var X, Y, Z : Real;

procedure Procedure1 (var M, N, Me : Real);

var X, You : Real;



end;{Procedure 1}

begin {ShowScope}

Procedure1(X, Y, Z)


Scope of X, Y, Z, Procedure1

Scope of M, N,

Me, X, You

Pascal Example


Parameters l.jpg


How we avoid side effects!


Parameters33 l.jpg


  • 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


Parameter communication l.jpg

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.


How parameters are used l.jpg

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.

Programmers now determine how their parameters are used.


Parameters36 l.jpg


  • 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


Actual parameters l.jpg

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

      • If they don’t, your program will blow up!

    • Can be a variable, constant or an expression

    • Can be call-by-value or call-by-reference


Formal parameters l.jpg

Formal Parameters

  • Is a list of “place marker” names used in the procedure’s declaration.

  • Parameters declared in the procedure header

  • Can include the data type of the valued parameters.

  • Must be a variable

  • Can be call-by-value or call-by-reference


Parameter correspondence rules l.jpg

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


Data areas after a call l.jpg

Formal Parameters

Actual Parameters









Local Variables

Sum Average



Data Areas After a Call

Main program data area Procedure data area


Valued variable parameters l.jpg

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.

    • You can use it AND you CAN change it!

  • 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.

    • You can use it BUT you CAN’T change it!


Call by reference l.jpg


  • 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


Call by value l.jpg


  • 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!


Parameter relationships l.jpg

Parameter Relationships

Call Statements

Actual Parameters

  • Valued

  • Variable

Parameter Interface using Global Variables

Procedure Header Parameters

Formal Parameters




Another look at variables l.jpg

Another Look at Variables

Names & the Data Dictionary


Variable names reminder l.jpg

Variable Names-Reminder

  • 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, Sales_Rate, PayRate, Temp


The data dictionary l.jpg

The Data Dictionary

  • Defines all of the variables used within a program

  • Lists:

    • Item Name

    • Variable Names

    • Domain(range of possible values)

    • Data type

    • Location defined & accessed

    • Test Data (or error checking)



Dd example l.jpg

DD Example


The three logic structures l.jpg

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


Slide50 l.jpg


Problem Solving

with Sequential Logic


  • Login