You are on page 1of 30

Chapter 4:

An Introduction to Programming Structure


Week 6 Day 1

Computer Science and IT | College of Computing | Hiba Al Senawi 1


The Week Agenda
Lecture One:
• Pointers for Structuring a Solution
• The Modules and Their Functions
• Cohesion and Coupling
• Local and Global Variables

Lecture Two:

• Parameters
• Return Values
• Variable Names and the Data Dictionary
• The Three Logic Structures

Computer Science and IT | College of Computing | Hiba Al Senawi 2


Objectives
When you have finished this chapter, you should be able to:
Explain the need for structured programming.

Explain how to design modules and functions in terms of cohesion and coupling.

Explain the difference between local and global variables.

Explain the use of parameters.

List and describe the three logic structures: sequential, decision, and loops.

Computer Science and IT | College of Computing | Hiba Al Senawi 3


Pointers for Structuring a Solution
You can develop efficient computer solutions to problems if you heed the following pointers
1. Use modules—break the whole into parts, with each part having a particular function.

2. Use the three 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.

1. The sequential structure

2. The decision structure

3. The loop structure

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.

Computer Science and IT | College of Computing | Hiba Al Senawi 4


The sequential
structure

Executes instructions one after


another in a sequence

Computer Science and IT | College of Computing | Hiba Al Senawi 5


The decision structure

Branches to execute one of


two possible sets of
instructions.

Computer Science and IT | College of Computing | Hiba Al Senawi 6


The loop structure
Executes a set of instructions many
times.

Computer Science and IT | College of Computing | Hiba Al Senawi 7


The Modules and Their Functions
• Follow these rules for designing modules when you design your own
solutions:
1. Each module is an entity in itself. There is one entrance and one exit. That is, the processing starts at the top
and ends at the bottom of the module.

2. Each module has a single function, such as printing, calculating, or entering data.

3. Each module is short enough to be easily read and modified.

4. The length of a module is governed by its function and the number of instructions to be executed to perform
that function.

5. A module is developed to control the order of processing.

Computer Science and IT | College of Computing | Hiba Al Senawi 8


The Modules and Their Functions
• The types of modules needed for the solutions to most problems in
this unit are the following:

1. The Control module shows the overall flow of the data through the program. All other modules are
subordinate to it.

2. The Initialization module processes instructions that are executed only once during the program, and only
at the beginning

Computer Science and IT | College of Computing | Hiba Al Senawi 9


The Modules and Their Functions
3. The Process modules may be processed only once, or they may be part of a loop, which is processed more
than once during the solution

a) Calculation modules
b) Print modules
c) Read and Data Validation modules

4. Wrap-up modules process all instructions that are executed only once during the program and only at the
end.

5. Modules in an object-oriented program may include event modules such as mouse down, mouse up, key
entry, and so on.

Computer Science and IT | College of Computing | Hiba Al Senawi 10


Cohesion and Coupling

• Cohesion is the ability for a module to work independently from all other
modules. Each module should have a function such as data entry, printing
information, specific calculations, and so forth.

• 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. Coupling allows for the communication between modules.

Computer Science and IT | College of Computing | Hiba Al Senawi 11


Cohesion and Coupling

Computer Science and IT | College of Computing | Hiba Al Senawi 12


Cohesion forms
1. Functional Cohesion
2. Sequential Cohesion
3. Communication Cohesion
4. Procedural Cohesion
5. Temporal Cohesion
6. Logical Cohesion
7. Coincidental Cohesion
See Page# 77 Ch#4

Computer Science and IT | College of Computing | Hiba Al Senawi 13


Local and Global Variables
• Local variables may be used only by the module itself. Other modules have no
knowledge of these variables. This allows cohesion to take place. Through the use of
local variables the programmer does not have to worry about variable name duplication in
modules created by other programmers.

• If other modules need to use the value of a variable, the modules must be coupled through
the use of parameters or return values. All modules have knowledge of global variables.

Computer Science and IT | College of Computing | Hiba Al Senawi 14


Local and Global
Variables

Computer Science and IT | College of Computing | Hiba Al Senawi 15


Homework #1

• Solve questions #1,2,3 and 4 chapter#4 page 87.

• Submit your answers through the Moodle.

• Due date : Thursday - October 21 , 2021

Computer Science and IT | College of Computing | Hiba Al Senawi 16


Thank you!
Coming Lecture Parameters
Return Values
Variable Names and the Data Dictionary
Chap. 4 Sec. 2 The Three Logic Structures

Computer Science and IT | College of Computing | Hiba Al Senawi 17


Chapter 4:
An Introduction to Programming Structure
Week 6 Day 2

Computer Science and IT | College of Computing | Hiba Al Senawi 18


Parameters
• 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.
• They are placed after the module name and within parentheses, for example: Read(A, B, C).
• The calling module is the module that processes another module.

Computer Science and IT | College of Computing | Hiba Al Senawi 19


Parameter

Computer Science and IT | College of Computing | Hiba Al Senawi 20


Actual Parameter Listing Formal Parameter Listing

• The actual parameter listing is the list of parameters that follows the module
name being processed in the calling module.

• The formal parameter listing is the list of parameters that follow the module name
at the beginning of the module.

Computer Science and IT | College of Computing | Hiba Al Senawi 21


Parameter

• There are two ways to send data from one module to another through the use of
parameters.

• The first is to send the value of the variable, a call-by-value parameter.

• The second is to send the address of the variable, a call-by-reference parameter


that is specified by the use of an asterisk (*) in front of the variable name in both
the actual and the formal parameter listings.

See Page # 81 & #82


Computer Science and IT | College of Computing | Hiba Al Senawi 22
Return Values
• The three ways to couple modules are through the use of global
variables, parameters, and return values.

• The return value is the result of the function. This is accomplished


through the name of the function.

• At the conclusion of the execution of the function, the result is placed


temporarily in the name.

Computer Science and IT | College of Computing | Hiba Al Senawi 23


Return Values
• The function must be used as part of another instruction.

• The value of the name of the function is no longer available once the
instruction has been executed

• In the case of the return value, the value is only sent out of the called
module into the calling module.

• In a coupling diagram the arrowhead would be a single-headed arrow, but


pointing to the calling module
Computer Science and IT | College of Computing | Hiba Al Senawi 24
Example

See Page # 84
Computer Science and IT | College of Computing | Hiba Al Senawi 25
Variable Names and the Data Dictionary

Computer Science and IT | College of Computing | Hiba Al Senawi 26


The Three Logic Structures
• The structure of the problem—that is, how to break a problem into parts, or modules, and arrange them
so the data flow in a solution is logical.

• There are other types of structures used to design the modules themselves.

• These structures are called logic structures, and they control the logic of the data flow though the
module.

• The instructions that make up the algorithms and the flowcharts are combinations of the four logic
structures.
1. The sequential logic structure
2. The decision logic structure
3. The loop logic structure

Computer Science and IT | College of Computing | Hiba Al Senawi 27


Summary
• Cohesion is designing a module to be as independent of the rest of the modules as possible.
• Coupling refers to how data connects the modules.
• The three ways to couple modules are through the use of global variables, parameters, and return values.
Global variables are those variables known to other modules.
• Local variables are those variables known to only one module.
• The computer searches for variables first in the local variable list, then the parameter list, and finally the
global list.
• Parameters are local variables that are shared with other modules.
• Call-by-reference parameters pass the address or location of the variable to the other module. Call-by-value
parameters pass the actual value of the variable to the other module.
• The calling module can recognize a change made by the called module when using a call-by-reference
parameter.

Computer Science and IT | College of Computing | Hiba Al Senawi 28


Homework #2

• Solve problem #1 and problem #2, chapter#4 page 87

• Submit your answers through the Moodle.

• Due date : Monday - October 25 , 2021

Computer Science and IT | College of Computing | Hiba Al Senawi 29


Thank you!
Coming Lecture
Chapter #5: Problem Solving with
Chap. 5 Sec. 1 the Sequential Logic Structure

Computer Science and IT | College of Computing | Hiba Al Senawi 30

You might also like