
-----------------------------------
Delos
Sun Mar 28, 2004 6:14 pm

[Tutorial]  Modules
-----------------------------------
MODULES:  A TUTORIAL

Hello all, and welcome to this tutorial on the ins and outs of modules!  I'm sure you're all just itching to get started, so let's.

What is a module?

A module is basically a collection or package of a number of procedures, functions, routines etc etc.  Modules are particularly useful for packaging algorithms that can be used over and over again.
Not all the contents of a module can be accessed from outside of it.  Which can and which can't is up to the programmer of that module.  Often, just a few elements of the module are exported, while the many others within it are used by the commands within those elements.

What does a module look like?

Here is a simplified version of modular declarations.  Some aspects have been omitted due to either their rarity or the lack of experience with them to aptly describe them.


module moduleName

     export export1 (,"¦)

     (
     procedure notExported
          -contents
     end notExported
     )

     procedure export1
          -contents
     end export1

end moduleName


A Step-By-Step Guide Through The Development Of A Module

Step 1:
Of course one needs and idea of sorts before coding begins.  This may be in the form of pseudo code, thoughts, whims, etc.  For this demonstration, we shall be making a simple string manipulator.
Our idea is to make an importable module that can do 3 things to text:
- turn it UPPER CASE
- turn it lower case
- turn it to aLtErNaTiNg CaSe

Step 2:
Begin the coding.  We know that we will have 3 main procedures/functions that we will be exporting.  For this demonstration, a bit of a lengthier and less inefficient means of coding will be used simply to show how modules may incorporate various containers.


module textMan

export toUpper, toLower, alt

procedure toUpper (var text : string)
end toUpper

procedure toLower (var text : string)
end toLower

procedure alt (var text : string)
end alt

end textMan


At the moment we have 3 stubs - declared procedures without any contents within them.  This is generally a good way to go about programming with modules as you will know exactly what is needed for each, which are done, which need work, etc, etc.
(Don't forget to save.  We're going to call this file "textMan.t".  What a coincidence.)

Step 3:
Continue coding.
For this demonstration the 3 exported procedures will not have any of the algorithms in them.  The manipulation will be carried out by functions, as previously stated, to show the module's versatility.

This will be added to the file after the export list.


    function makeUpper (var text : string) : string
        var temp : string := ""
        % Temporary variable used so as not to alter
        % 'text' until the final step.
        for i : 1 .. length (text)
            if ord (text (i)) >= 97 and ord (text (i)) = 65 and ord (text (i)) = 97 and ord (text (i)) = 65 and ord (text (i)) 