INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1
Dec 27, 2015
INTRODUCTION TO PROGRAMMING
STRUCTURE
Chapter 4
1
Overview2
Pointers Modules & Functions Cohesion & Coupling Local & Global Variables Parameters Variable Names & Data Dictionaries Three Logic Structures
Pointers for Structuring a program
3
Use Modules Each part should have a particular function
Use the four logic structures Sequential , Decision , loop & case
Eliminate the rewriting of identical processes by using modules .
Use techniques to improve readability.
4
- The computer should be a tool to help people find
solutions to their problems and to increase their
productivity.
- You can develop efficient computer solutions to
problems if you heed the following pointers:
1- Use modules. Break the whole into parts, each part
having a particular function.
Pointers for Structuring a program
5
2- Use the four logic structures to ensure that solution flows smoothly from one instruction to the next, rather than jumping from one point in the solution to another.
a- The sequential structure executes instructions one
after another in a sequence.
b- The decision structure branches to execute one of
two possible sets of instructions.
c- The loop structure executes a set of instructions
many times.
d- The case structure executes one set of instructions
out of several sets.
Pointers for Structuring a program
6
Sequential Logic Structure
7
Decision Logic Structure
8
Loop Logic Structure
9
Case Logic Structure
10
rules for designing modules Each module
is an entity by itself . There is one entrance and one exit , the processing does not jump out of the middle of a module to another module.
has a single function ( printing , calculating , entering data)
should be easily read, modified ( short enough).
Length is governed by function and number of instructions contained within
Controls the order of processing.
11
Types of modules 12
Control modules . Initialization module Process module ( process only once or
loop): calculation modules . print modules . read and data validation modules.
Wrapup modules
Control Modules
Most often called “Main” shows the overall flow of the data
through the program All other modules are subordinate to the
control module.
13
Initialization Module
that are executed only once during the program and only at the beginning.
Examples Opening files Initializing variables ( beginning values )
14
Process Modules May be processed only once , or the may
be part of a loop , there are several kind : Calculation modules:
arithmetic operations Accumulations Counting Manipulate string data.
Read and Data Validation modules: Reads and validates input data
Usually separate modules Print modules :
print output line( the result of the processing)
15
Wrap Up Module
process all instructions that are executed once during the program and only at the end Examples
Closing files Printing totals .
16
Modules17
Modules are often combined in one solution .
A module can be broken into several smaller modules to make the parts of a program more manageable.
One module performing a specific function can be used by one or more other modules .
COHESION & COUPLING
chapter 4
18
Cohesion & Coupling19
Each module should Be functionally independent. Perform single task . Modules will need to connected ,primarily
through the data needed to complete the specified task within the modules ( cohesion and coupling ) .
cohesion20
The ability for a module to work independently from all other modules ( single entry and single exit).
Each module should have a function . Each module should have single entry
and single exit.
coupling21
modules need to share data from other modules in order to complete the modular tasks.
is accomplished by some type of interface between modules that enables data to be passed from one module to another with the minimum interruption of the modular independence .
Allows for the communication between modules. There are three ways to couple modules : global
variable , parameters , and return value .
LOCAL AND GLOBAL VARIABLES
Programmer use the concept of local and global variables to allow cohesion and coupling to occur.
Global variable( define outside individual module)
Local variables ( define within module) The difference between the two is in the
scope of the variables .
Local variables24
May be used only by the module itself. Other modules have no knowledge of
these variables. Allow cohesion to take place . The programmer does not have to worry
about variable name duplication in modules created by other programmers.
The modules can be coupled through the use of parameters or return values.
global variables25
thy can be seen by all modules. Allow data coupling through the entire
program . The programmer must be careful not to
use the same variable name for local variable and a global variable .
The use of parameter and return value is not necessary since global variable couple the data from module to module .
global variables
Can be referenced anywhere in the program Is visible and accessible
everywhere
X, Y, Z
A
CB
X, Y & Z are Global to modules
A, B & C
Interactivity chart27
Scope of Local and Global Variables28
Parameters29
Local variables that are passed from one module to another .
Another way of coupling that allow the communication of data between modules.
Thy are placed after module name between parentheses.
Read ( A , B , C)
Parameters30
Make modules more versatile. Different data can be manipulated each
time the module is called. The calling module (module that process
another module) The called module ( the module being
processed).
Parameter Terminology31
Parameters32
Parameters Comes in two types: Actual (is the list of parameter that follows the
module name being processed in the calling module )
Formal (is the list of parameter that follows the module name at the beginning of the module ).
The variable name in the actual may or may not be the same as those in the formal parameter .
The value are sent and received according to the position in the listings .
Actual & Formal Parameters
33
Actual Parameters Parameters used in the call statement 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
Formal Parameters Must be a variable Can be call-by-value or call-by-reference
Calling data through the modules There are two ways to send data from
one module to another: Send the value of the variable ( call-by-
value) Send the address of the variable (call-by-
reference) Specified by the use of an asterisk (*) in front of
the variable name ( actual and formal parameter)
Call-by-Reference35
the memory location is sent ( the address) Is specified by the use of an asterisk( * ) in
front of the variable name in both the actual and formal .
Gives access to the contents of the storage area where values are stored.
When the called module change the value of the parameter , the calling module will see the change. They are using the same memory location.
Call-by-Value36
the value of the variable is sent to the called module by the calling module.
Accomplished by creating a copy of the value
The called module will make a new memory location for this variable .
When the called module makes a change in the variable ,the change will not be made in the calling module. Parameter have different memory location
Parameters – Call-by-value and Call-by-Reference
Parameters – Call-by-value and Call-by-Reference
Parameters – Call-by-value and Call-by-Reference
Parameter Example
Coupling diagram
A coupling diagram indicates how data couples modules together
Has a rectangle for each module . Line connecting the modules for each
datum sent from one module to another . The double-headed arrows indicate that
these parameters are call - by - reference . The single- headed arrows indicate that
these parameters are call-by-value .
Coupling diagram
Coupling diagram
parameters
Parameter in the formal parameter listing are neither local nor global variables .
Should not be declared as local or global variables.
In the event of variable name duplication , the local variable has precedence over the parameter variable .
The parameter variable has precedence over the global variables .
Return value 45
Another way to couple modules . The return value is the result of the function . This is accomplished through the name of
the function. The result is placed temporarily in the name. The function must be used as parts of
another instruction. SquareRoot = 5 + SQRT ( 16 )
The value of SQRT is no longer available to be used
Variable Names and the data dictionary
46
Use a variable name that relates the name of the variable to its usage.
Variable name must be unique to the module in thy are defined.
Global variable must be unique to the total program .
The computer references values by their names .
Data dictionary is a great help to keep track of the variable usage in your program .And Defines all of the variables used within a program.
Example of a Data Dictionary
The four Logic Structures
48
control the logic of the data flow through the module.
The four basic logic structures : Sequential logic structure Decision logic structure . Loop logic structure . Case logic structure .