Beruflich Dokumente
Kultur Dokumente
Modularity
Modules and Modularity 5th form
The programs we have written so far are simple with not too many lines of code. However,
programs are seldom written as one long series of steps. As programs increase in complexity, it
becomes more and more difficult to consider the solution as a whole. Instead, the problem is
divided into smaller parts or units, referred to as modules. Modules are as a result of a structured
design style (modularity) that breaks down (decomposes functionally or otherwise) a program
into distinct portions (modules), each of which, ideally, accomplishes a single task or procedure.
To break up a problem into modules, the following steps should be taken:
- identify the major tasks to be performed then divide the problem into sections that represent
those tasks
- look at each subtask and identify within them further subtasks and so on
This process of identifying first the major tasks, then further subtasks within them is known as
top-down design or functional decomposition. Each subtask will eventually become a module
within a program. Top-down design methodology allows the programmer to concentrate on the
overall design of the algorithm without getting too involved with the details of the lower-level
modules (abstraction).
A module must be large enough to perform its task and must include only the operations that
contribute to the performance of that task. The name of the module usually indicates the task of
function that the module is to perform, for example, calculate_sales_tax or validate_input_data.
Benefits:
-
Hierarchy Charts
A hierarchy or structure chart is a diagrammatic representation of the hierarchical relationship of
modules to each other (similar to an organizational chart within a company). It is also referred to
as a structure chart. At the top of the hierarchy chart is the main module. On the next level are the
modules immediately subordinate to the main module. On the next level are the modules
Module1
Module3
Module2
Module
Module
2a
2b
STOP
We have only one program main, in which all instructions are defined. However, if we used
modularity, where we have one module called by main to do the program then we would have:
START
CALL oddAndEven
STOP
The module oddAndEven has a particular task to be completed. The task in this case is to
display all the odd and even numbers between 1 and 10. This task has to be clearly defined. The
definition of this module is NEVER done inside the main module. It is done outside, usually at
the end of the main program.
PROCEDURE oddAndEven
ENTER
DECLARE count as INTEGER
FOR count = 1 to 10 DO
IF count %2 = 0 THEN
WRITE count is even
ELSE
WRITE count is odd
ENDIF
ENDFOR
RETURN
The main module in the above example does nothing more than call the subordinate module
oddAndEven to do the work. The oddAndEven module is called a procedure. A procedure is a