``

prev Menu next

Modules ☆
The mechanism of modules enables sharing variables among some program units.

1. A Module contains public variables, and share variables.
Public variables are shared with the whole module and the program units where the external declaration for that variable is written.
(Note that a program unit is an external procedure or the main program.)
Share variables are shared with the whole module.
2. Example. (Share variables)
On the following program, the numeric variables 'index' and 'stack' defined in the 2030-line are shared with the module 'stack'.

    1000 DECLARE EXTERNAL SUB stack.push
    1010 DECLARE EXTERNAL FUNCTION stack.pop
    1020 CALL push(12)
    1030 PRINT pop
    1040 END
    1050 !
    2000 MODULE stack
    2010 PUBLIC SUB push
    2020 PUBLIC FUNCTION pop
    2030 SHARE NUMERIC index, stack(100)
    2040 LET index=0
    2100 EXTERNAL SUB push(a)
    2110    LET index=index+1
    2120    LET stack(index)=a
    2130 END SUB
    2200 EXTERNAL FUNCTION pop
    2210    LET pop=stack(index)
    2220    LET index=index-1
    2230 END FUNCTION
    2240 END MODULE

3. A module consists of the module-body and external procedures.
The module-body can contain public declarations and share declarations.
The module-body shall be executed before the main program.
PUBLIC declarations declares the variables or procedures shared with the program.
`PUBLIC NUMERIC numeric_variable, numeric_variable , ...
PUBLIC STRING string_variable, string_variable , ...
PUBLIC FUNCTION function_name, function_name, ...
PUBLIC SUB subprogram_name, subprogram_name, ...
PUBLIC PICTURE picture_name, picture_name, ...
`
SHARE declarations declare the variables shared within the module.
`SHARE NUMERIC numeric_variable, numeric_variable , ...
SHARE STRING string_variable, string_variable , ...
SHARE FUNCTION function_name, function_name, ...
SHARE CHANNEL channel-number, channel_number, ...
` (channel_numbers are as #1, #2 and so on.)
For an array declaration, the bounds-declaration is included.
Example.

    MODULE m1
    PUBLIC NUMERIC a(2,2), b
    PUBLIC STRING s$(4)
    SHARE NUMERIC c(10)
    LET b=pi/4
    　…………
    END MODULE

4. Module-option statement can be written in the module-body.
EXAMPLE.
`MODULE OPTION ANGLE DEGREES
MODULE OPTION ARITHMETIC NATIVE
` Any module option statement is valid through the whole module.
[note]
(1) OPTION declaration written in a module-body is valid only through the module-body.
(2) Neglecting the standard, MODULE OPTION statements and OPTION statements must be written before any PUBLIC statement or any SHARE statement.
(3) The module-body of a module cannot contain any internal procedure.

5. A Program unit that has references to public-declared procedures or variables must contain the DECLAE EXTERNAL declarations for those references.
`DECLARE EXTERNAL NUMERIC module_name.numeric_variable, module_name.numeric_variable, ...
DECLARE EXTERNAL STRING module_name.string_variable, module_name.string_variable, ...
DECLARE EXTERNAL FUNCTION module_name.function_name, module_name.function_name ,…
DECLARE EXTERNAL SUB module_name.subprogram_name, module_name.subprogram_name, ...
DECLARE EXTERNAL PICTURE module_name.picture_name, module_name.picture_name, ...
`
If an arrays is specified, parenthesis and commas of number of dimension minus 1 follows the array name.
On the program unit that declares external procedures or variables, the procedures or variables can be written with or without module name part.
Example

    DECLARE EXTERNAL NUMERIC m1.a(,), m1.b
    DECLARE EXTERNAL STRING m1.s$()
    FOR i=1 TO UBOUND(s$)
       PRINT s$(i)
    NEXT i
    PRINT m1.a(1,1), m1.b
    ………
    END

For more information about modules, refer to ANSI X3.113a-1989