Languages 5.4 Modules (Procedures / Functions)

Modules

When dealing with solutions to problems, it is a good idea to keep the algorithms that relate to each process isolated from that of other processes. An algorithm that consists of 25- 30 lines usually is sufficient to define a given process. Thus, to make good solutions to problems, separate ideas should be kept apart and placed into separate modules.

Some of the major advantages of modularisation are:

When designing a solution to a problem, the first task is to determine the major processes involved in creating a solution. Then in turn, each one of these processes is broken down into its sub-process. This mechanism is continued, breaking each module down into smaller and smaller modules until the basic idea and amount of code does not require any more than about 30 lines of instructions to define it.

The result will be a hierarchical chart that shows the breakdown of each module and sub-module. This process is known as a TOP-DOWN approach to algorithm design.

Global Variable

As a consequence of the modularization approach to programming, data allocated to variables in one module may be required in another module. One method of providing this data is to define global variables that hold common data. These are variables, with attached data, that are available to all modules throughout the algorithm (program).

However, there are some major disadvantages in using global variables. The first is that if one module changes the value and another does not expect it to be changed, then errors will occur. This is called a side-effect. The other is that it becomes very difficult for teams of programmers to work independently as they all need to know the name of the variables used by each other, and must be very careful not to duplicate them.

Problem

Write a program in modular form that accepts a person's name and age and then shows that information.

Here is the algorithm to this example in pseudocode and written in a modular form with the global variables Name and Age.

Module GetData
READ Name
READ Age
Module ShowData
WRITE Name
WRITE Age
Module ChangeData
SET Age to 4
Mainline
GetData
ChangeData
ShowData

The result of this algorithm is that Age will always be 4! The module Changedata changes the value of the global variable Age.

Passing Parameters

A parameter is a variable with an attached value that is associated with, or is local to, a module. Parameters within a module allows the module designer to come up with any word they like for the variable names of their data set and also only accept data from other modules that comply with the way and data type they request. parameters may be passed into and outof a module.

Problem (Same as above)

Write a program in modular form that gets a person's name and age and then shows that information.

Here is the same algorithm to the problem above written in pseudocode and in a modular form with parameters and global variables that just temporarily hold data.

Module GetData(Name, Age)
READ Name
READ Age
Module ShowData(XName, XAge)
WRITE XName
WRITE XAge
Module ChangeData
SET Age to 4
Mainline
GetData(SomeName, SomeAge)
ChangeData
ShowData(SomeName, SomeAge)

The result of this algorithm is that the age displayed will always be the age value entered. This is because Age in ChangeData is defined locally within the module and not globally within the program. Thus changing Age in ChangeData has no effect on any other variable called Age, or associates with Age as the value is local only to the module ChangeData.

In the case of the above algorithm, module GetData passes the values of Age and Name out of the module to make it available to other modules. The module ShowData takes values of Age (SomeAge) and Name (SomeName) in displays it. (Little arrows showing direction on the data names of each module help demonstrate this - Unfortunately this is not able to be displayed in HTML)

Note also that the names of the variables can also be completely different between modules, so long as the mainline sends the correct data to each module. Any Variable declared as part of the module is local to that module only, unless passed out on purpose.

Note: When writing prgrams in pascal, Value Parameters allow data to be passed into a module while Variable Paramaters allow data to be passed out of modules.

Pascal program for Algorithm

Program ParamDemo;
Type
  String20 = String[20];
Var
  SomeName : String20;
  SomeAge : Integer;
Procedure GetData(Var Name : String20; Var Age : Integer);
Begin
  ReadLn(Name, Age)
End;
Procedure ShowData(XName : String20; XAge : Integer);
Begin
  WriteLn(XName, XAge)
End;
Procedure ChangeData;
Var
  Age : Integer
Begin
  Age := 4
End
Begin
  GetData(SomeName, SomeAge);
  ChangeData;
  ShowData(SomeName, SomeAge) 
End.

Author Mike Leishman