Lesson 4
Lesson 4
2
When you have finished this chapter, you
should be able to:
1.Explain how to design modules and functions in
terms of cohesion and coupling.
2.Explain the difference between local and global
variables.
3.Explain the use of parameters.
3
The tools described in Chapter 3 help
programmers organize programs as they
arise in the business world.
While
The techniques explained in Chapters 4
4
You can develop efficient computer solutions to problems
if you heed the following pointers/techniques:
1.Use modules—break the whole into parts, with each
part having a particular function.
2.Use the four logic structures to ensure that the solution
flows smoothly from one instruction to the next, rather
than jumping from one point in the solution to another.
The sequential structure executes instructions one
after another in a sequence.
The decision structure executes instruction based on
whether the given condition is true or false.
The loop structure executes a set of instructions many
timesrepeateadly.
The Case logic/ Switch structure, executes
instructions based on a set constant value
5
3. Eliminate the rewriting of identical
processes by using modules.
4. Use techniques to improve readability,
including the four logic structures, proper
naming of variables, internal documentation,
and proper indentation
6
7
8
9
10
Just as an author would not sit down and write
without thinking about the structure of the book,
neither should a programmer write without
thinking about the structure of the program. First,
an author thinks about the subject. A programmer
studies the problem. Next, the author breaks the
subject matter into chapters. The programmer
breaks the problem into modules, each with a
specific function. Once the chapters are in the
correct order, then the author starts to write
them, one by one. Once the modules are in the
correct processing order in the interactivity chart,
then the programmer writes and tests the
algorithm for each module, one by one.
11 11
Each module
1. 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.
2. has a single function (Eg we can create a
module for printing , or calculating , or entering
data)
3. should be easily read, modified ( short enough).
4. Length is governed by function and number of
instructions contained within
5. Controls the order of processing.
12 12
Control modules .
Initialization module
Process module ( process only once or
loop):
◦ calculation modules .
◦ print modules .
◦ read and data validation modules.
Wrapup modules
13
Most often called “Main”
This is a module that shows the overall flow
control module.
14 14
This is a module that includes instructions
that are executed only once during the
program and only at the beginning.
15 15
These are modules that contain instructions
for performing a given task. They may be
processed only once , or that may be part of
a loop , there are several kinds of process
modules :
Calculation modules: These are modules that do;
arithmetic operations
Accumulations (summing to calculate totals),
Counting
Manipulate string or numerical data.
Read and Data Validation modules: These are modules
that reads and validate input data. Validation modules
are usually separate from Read modules.
Print modules : These are modules used to output the
results of the processing.
16 16
This is a module that includes all
instructions that are executed once during
the program and only at the end.
17 17
Modules are often combined in one
solution .
18
One of the most difficult tasks in the
development of a solution to a problem is to
divide the solution into modules. The
question arises as to what should be
included in each module.
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 ) .
19
The ability for a module to work
independently from all other modules
( single entry and single exit). Or It can be
defined as the measure of connectivity of
elements of a single module.
Each module should have a function .
Each module should have single entry and
single exit.
20
Coupling refers to the ability of modules to
share data in order to complete the modular
tasks.
Coupling 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 .
There are three ways to facilitate coupling
between modules :
◦ global variable ,
◦ parameters , and
◦ return value .
21
22
Programmers use the concept of local and
global variables to allow cohesion and
coupling to occur.
Global variable( These are variables defined
within a module)
23
These are variables defined within a
module
They are used only by the module itself.
Other modules have no knowledge of these
variables.
Allow cohesion to take place .
24
These are variables defined outside
individual modules
They can be seen by all modules (because it
program .
The programmer must be careful not to use
25
Can be referenced anywhere in the
program
◦Is visible and accessible
everywhere
A
X, Y & Z are
X, Y, Z Global to modules
B C A, B & C
26
27
Local variables that are passed from one
module to another .
Another way of coupling that allow the
parentheses.
Read ( A , B , C)
28
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).
29
30
Parameters are local variables that are passed or
sent from one module to another.
Parameters are another way of facilitating coupling
that allows the communication of data between
modules.
Parameters Comes in two types:
◦ Actual parameters: These are parameters
containing actual values to be passed to a module
(They are defined when calling the module)
◦ Formal parameters: These are the place
holders for values expected by the module. They
are defined when creating the module.
The variable name in the actual may or may not be
the same as those in the formal parameter .
31
Actual Parameters
◦ These contain the actual values needed by
modules to complete tasks
◦ Parameters used in the call statement
◦ Data types must be assignment compatible with
its corresponding formal parameter
Formal Parameters
◦ Must be a variable
32
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)
33
By this method, the memory location of the
value is sent ( the address) and later the
value got from that memory location.
Is specified by the use of an asterisk( * ) in
34
By this method, the value of the variable is
sent to the called module by the calling
module.
35
36
37
Another way to couple modules .
The return value is the result of the function .
At the conclusion of a module execution, the
38
39
Use a variable name that relates the name of the
variable to its usage.
Variable name must be unique to the module in
which they 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.
40
41
Thanks for listening
QUESTIONS??????????????