Packages

A package is a block that can contain only declarations, default statements, and procedure blocks. The package forms a name scope that is shared by all declarations and procedures contained in the package, unless the names are declared again.

Some or all of the level-1 procedures can be exported and made known outside of the package as external procedures. A package can be used for implementing multiple entry point applications.

A package that contains a MAIN procedure must not contain any FETCHABLE procedures. A package that contains a MAIN procedure must not be linked into a DLL. It should form part of a base executable that can, if desired, invoke routines in a DLL. Such a package can, of course, also define external routines that can be called from other routines statically linked with it, and the package can also define EXTERNAL STATIC data that can be referenced from other routines statically linked with it.

If a package that does not contain a MAIN routine is linked into a DLL, the only EXTERNAL STATIC variables that will be exported from that package out of the DLL are those variables that have the RESERVED attribute.

If the source contains a PACKAGE statement, there must be at most only one set of *PROCESS statements and those must be the first statements in the source. If the source contains no PACKAGE statement, the compiler effectively inserts one after the first set of *PROCESS statements and the source might contain multiple external procedures separated by groups of *PROCESS statements.

If two packages are linked together, then they must export disjoint sets of names.

Read syntax diagramSkip visual syntax diagramcondition-prefix:package-name:PACKAGEEXPORTS(,procedure*)RESERVES(variable-nameEXTERNAL( enviornment-name))OPTIONS( options);declare-statementdefault-statementprocedure-statementENDpackage-name ;
procedure
Read syntax diagramSkip visual syntax diagramprocedure-name EXTERNAL( environment-name)
condition-prefix
Condition prefixes specified on a PACKAGE statement apply to all procedures contained in the package unless overridden on the PROCEDURE statement. For more information, see Condition prefixes.
package-name
The name of the package. All PACKAGE names must be unique within a linked module.
EXPORTS
Specifies that all (EXPORTS(*)) or the named procedures are to be exported and thus made externally known outside of the package. If no EXPORTS option is specified, EXPORTS(*) is assumed.
procedure name
Is the name of a level-1 procedure within the package.
EXTERNAL (environment name)
Is a scope attribute discussed in Scope of declarations.
RESERVES
Specifies that this package reserves the storage for all (RESERVES(*)), or only for the named variables that have the RESERVED attribute. See RESERVED attribute.
variable name
Is the name of a level-1 external static variable.
OPTIONS option
For OPTIONS options applicable to a package statement. See OPTIONS option and attribute.
declare statement
All variables declared within a package but outside any contained level-1 procedure must have the storage class of static, based, or controlled. Automatic variables are not allowed. Default storage class is STATIC. See Data declarations.
default statement
See Defaults for attributes.
procedure statement
See PROCEDURE statement.

Example of package statement

Package statement
*Process S A(F)  LIMITS(EXTNAME(31)) NUMBER;
 Package_Demo: Package exports (Factorial);
 
 /***********************************************/
 /*                 Common Data                 */
 /***********************************************/
 
 dcl N fixed bin(15);
 dcl Message char(*) value('The factorial of ');
 
 /***********************************************/
 /*                 Main Program                */
 /***********************************************/
 
 Factorial: proc options (main);
    dcl Result fixed bin(31);
    put skip list('Please enter a number whose factorial ' ||
                  'must be computed ');
    get list(N);
    Result = Compute_factorial(n);
    put list(Message || trim(N) || ' is ' || trim(Result));
 end Factorial;
 
 /***********************************************/
 /*                  Subroutine                 */
 /***********************************************/
 
 Compute_factorial: proc (Input) recursive returns (fixed bin(31));
    dcl Input fixed bin(15);
    if Input <= 1 then
       return(1);
    else
       return( Input*Compute_factorial(Input-1) );
 end Compute_factorial;
 
 end Package_Demo;