Microsoft KB Archive/35563

From BetaArchive Wiki

Example of Using Pascal Modules PSS ID Number: Q35563 Article last modified on 04-16-1991 PSS database name: S_PasCal

3.3x 4.00 | 4.00



The following information provides an example of using Modules in Pascal for multiple Pascal source-file programs.

More Information:

Below is the MAIN program file. It calls procedures in a module file named “mod_2.pas”. The extern attribute is used to import global variables, procedures, or functions declared as public in other modules. For variables, attributes are enclosed in square brackets following the variable names and preceding their type declarations. For procedures or functions, the extern attribute goes at the end of the formal declaration, as follows:

{Note that the global identifier names must be the same in all modules including main. }

program MainProgram(input,output); var passedvar [public], {public variable to be imported by module} number1, {variables local to this file} number2 : integer; value PassedVar := 7; {initialize variable in value section}

{declare procedures as “extern” to indicate that they are defined in an external module.}

procedure PrintImportedVar; extern; procedure adding(n1, n2 : integer); extern; procedure multiplying(n1, n2 : integer); extern;

begin writeln; write(‘Enter an integer :’); readln(number1); writeln; write(‘Enter another integer :’); readln(number2); writeln; {these procedures are in the} adding(number1,number2); {module file “implemen.pas”} multiplying(number1,number2); PrintImportedVar; writeln end.


{This is the module calculate. It contains the procedures called by main. Note that it does not have a “begin..end” body because only the main program module can have a body.

The integer “PassedVar” is imported from main by the [extern] variable declaration below which corresponded to the [public] declaration of “PassedVar” in main. }

MODULE calculate [PUBLIC]; {module heading}

{The module begins with the MODULE heading. [PUBLIC] indicates that global variables (or functions or procedures as well) are defined in this module, and may be used by other modules including main if they are declared as extern. }

var passedvar [extern] : integer;

procedure PrintImportedVar; begin writeln(‘The imported variable should be 7, is’,passedvar) end;

procedure adding(num1, num2 : integer); var sum : integer; begin sum := num1 + num2; writeln(‘Their sum equals’,sum) end;

procedure multiplying(num1, num2 : integer); var prod : integer; begin prod := num1 * num2; writeln(‘Their product equals’,prod) end;

end. {end of module calculate}

Copyright Microsoft Corporation 1991.