modula-2 home

  Home  
  Tutorial  
  Win32 API  
  Reference  
  Projects  
 

 

Standard procedures

The Modula-2 language includes a set of standard procedures that are built into the compiler and therefore are always available without importing them.

This chapter lists the standard procedures, their uses, allowed parameter types, and the type returned, if any.

ABS

PROCEDURE ABS(X : type) : type;

The absolute value function.  X can be any signed integer type, REAL, LONGREAL, COMPLEX or LONGCOMPLEX.  The result is the same type as the parameter.

The absolute value of a positive number is itself, and the absolute value of a negative number is the negative of the number.

The absolute value of a complex number is its magnitude. (Remember magnitude is defined as SQRT(real2 + imaginary2))

CAP

PROCEDURE CAP(ch : AnyCharType) : AnyCharType;

Converts ch to uppercase.  Characters other than lowercase letters in are left unchanged by this function.

For operating systems that support many languages this function returns correct results for the language the operating system has been setup to use.

CHR

PROCEDURE CHR(val : type) : CHAR;

Converts a numeric value to a value of type CHAR.  The value can be any signed or unsigned integer type.  The result is the character whose ordinal value is val.

If assignment checking is on, val is checked for a value in the range of type CHAR.

DEC

PROCEDURE DEC(VAR v : type);

PROCEDURE DEC(VAR v : type; amount : CARDINAL);

Decreases the value of variable v by one or by a specified amount. If an amount is not passed a value of one is assumed for amount. The variable can be of any signed or unsigned integer type, any character type, any boolean type or any enumeration type. The amount must be assignable to CARDINAL.

DISPOSE

PROCEDURE DISPOSE(VAR P : PointerType {;tag : tagtype});

Releases the storage allocated to a pointer by the NEW standard procedure or the ALLOCATE procedure.

The optional tag expressions are the tags of variant parts in a record that is being disposed.  Each tag expression must be a compile-time constant. The values of the tags must be identical to those given when the object was allocated.

When you dispose of dynamic arrays you do not supply the array HIGH bounds to DISPOSE as you did with NEW. The compiler will fetch these values automatically for you from the dynamic array data.

A call to DISPOSE is translated to a call to DEALLOCATE. In order to use DISPOSE, you must define the procedure DEALLOCATE.  Usually, you import DEALLOCATE from the ISO module Storage. You can, however, define the procedure yourself.

EXCL

PROCEDURE EXCL(SetVar : SetType; Element : ElementType);

Excludes an element from a SET or PACKEDSET variable. SetVar must be a variable of a set type and Element must be an expression resulting in a value of the element type of the set. The specified element is removed from the value of the set variable.

FLOAT

PROCEDURE FLOAT(num : AnyIntegerType) : REAL;

Converts the integer number num to REAL. num can be any signed or unsigned integer type.

LENGTH

PROCEDURE LENGTH(str : ARRAY OF AnyCharacterType) : CARDINAL;

Returns the length in characters of the passed string. The string passed to length can be an ANSI or Unicode string.

LFLOAT

PROCEDURE FLOAT(num : AnyIntegerType) : LONGREAL;

Converts the integer number num to LONGREAL. num can be any signed or unsigned integer type.

HALT

PROCEDURE HALT;

PROCEDURE HALT(status : CARDINAL);

Halts the execution of a program.  Use the second form to return a status to the operating system.  This is useful to inform the program that executed the current program about the success or lack thereof of the execution.  If no status is given, 0 is the default value is returned.

HIGH

PROCEDURE HIGH(ArrayVar : ArrayType) : CARDINAL;

Returns the upper bound of an open array parameter variable.

In extended syntax mode you can pass any array variable to the HIGH function.

You can access all dimensions of an array with more than one dimension by passing the appropriate array dimension to HIGH. For example, to get the high bound of the second dimension of an array type with two or more dimensions you would subscript the array variable with one index, thus leaving the second dimension. The value used to index the array is not used since you are not accessing an array element but are informing the compiler which array HIGH dimension to retrieve. Typically you would use the value 0 for the subscript.

Example:

PROCEDURE test(arr : ARRAY OF ARRAY OF REAL);
.
.
.
   FOR i := 0 TO HIGH(arr) DO (* first dimension )
       FOR j := 0 TO HIGH(arr[0]) DO ( second dimension *)

INC

PROCEDURE INC(VAR v : type);

PROCEDURE INC(VAR v : type; amount : CARDINAL);

Increases the value of variable v by one or by a specified amount.  If an amount is not passed a value of one is assumed for amount. The variable can be of any signed or unsigned integer type, any character type, any boolean type or any enumeration type.  The amount must be assignable to CARDINAL.

INCL

PROCEDURE INCL(SetVar : SetType; Element : ElementType);

Includes an element in a SET or PACKEDSET variable.  SetVar must be a variable of a set type, and Element must be an expression resulting in a value of the element type of the set.  The specified element is added to the value of the set variable.

INT

PROCEDURE INT(Val : ValType) : INTEGER;

Converts Val to INTEGER.  Val can be any signed or unsigned integer type, any character type, a boolean type or any enumeration type.  For character, boolean and enumeration types, the returned value is the ordinal value of the parameter.

MAX

PROCEDURE MAX(ScalarType) : ScalarType;

Returns the maximum value of a scalar type.  The parameter is a type name, which must be a signed or unsigned integer type, a real type, a character type, a boolean type, a subrange, or an enumeration type.  The type returned by MAX is the same as the type of the parameter.

MIN

PROCEDURE MIN(ScalarType) : ScalarType;

Returns the minimum value of a scalar type.  The parameter is a type name, which must be signed or unsigned integer type, a real type, a character type, a boolean type, a subrange, or an enumeration type.  The type returned by MIN is the same as the type of the parameter.

NEW

PROCEDURE NEW(VAR P : PointerType {;tag : tagtype});

PROCEDURE NEW(VAR P : DynamicArrayType; highBounds : CARDINAL
            {; highBounds : CARDINAL});

The optional tag expressions are the tags of variant parts in a record that is being allocated.  Each tag expression must be a compile-time constant.  The values of the tags determine how much storage is allocated.

For dynamic arrays you must specify a high bound for each array dimension declared in the dynamic array declaration. Remember that you are specifying the high bound for the dimension, and the lower bound is assumed to be zero, and therefore the size of each dimension is the highbound+1.

Example:

PROCEDURE AllocateMatrix(VAR m : Matrix; order : CARDINAL);
BEGIN
   (* order is the number of element in each dimension )
   ( therefore the high bound is one less than this )
   ( square matrix *)
   NEW(Matrix, order-1, order-1);
END AllocateMatrix;

A call to NEW is translated to a call to ALLOCATE. In order to use NEW, you must define the procedure ALLOCATE. Usually, you import ALLOCATE from the ISO module Storage. You can, however, define the procedure yourself.

ODD

PROCEDURE ODD(Val : ValType) : BOOLEAN;

Returns TRUE if Val is ODD and FALSE otherwise.  Val can be any signed or unsigned integer type.

ORD

PROCEDURE ORD(Val : ValType) : CARDINAL;

Converts Val to CARDINAL.  Val can be INTEGER, LONGINT, LONGCARD, CHAR, BOOLEAN, or any enumeration type.  For CHAR, BOOLEAN and enumeration types, the returned value is the ordinal value of the parameter.

RE

PROCEDURE RE(Val : COMPLEX) : REAL;

PROCEDURE RE(Val : LONGCOMPLEX) : LONGREAL;

Returns the real part of a complex type.

IM

PROCEDURE IM(Val : COMPLEX) : REAL;

PROCEDURE IM(Val : LONGCOMPLEX) : LONGREAL;

Returns the imaginary part of a complex type.

CMPLX

PROCEDURE CMPLX(realPart, imaginaryPart : REAL) : COMPLEX;

PROCEDURE CMPLX(realPart, imaginaryPart : LONGREAL) : LONGCOMPLEX;

Returns a complex type where realPart and imaginaryPart compose the complex type. Note that this is the same syntax used to compose a complex constant. If realPart and imaginaryPart are constants then CMPLX returns a complex constant compatible with a complex types. If realPart or imaginaryPart are not compile time constants then CMPLX can only be used in a statement.

SIZE

PROCEDURE SIZE(item) : IntConst;

Returns the number of bytes of storage occupied by item.  item can be either a variable or a type name.  Variables can be fully qualified with subscripts or record field references. The type returned by this procedure is compatible with signed and unsigned integer types.

TRUNC

PROCEDURE TRUNC(RealVal : RealType) : CARDINAL;

Converts a REAL or LONGREAL value to CARDINAL by truncation; the fractional part of the real number is discarded.  The result is undefined if RealVal is out of the range of CARDINAL.

VAL

PROCEDURE VAL(ConvertToType; Value : AnyPervasiveType) : Type;

Converts a value from one type to another.  This function can convert  any predefined pervasive type to any other pervasive type. With the following exceptions.

  • You cannot convert an enumeration type to a real or complex type. Remember that boolean and character types are defined as enumeration types.
  • You cannot convert a real type to a complex type.
  • You cannot convert a complex type to a real type.

The following are TRUE:

   INT(...)       = VAL(INTEGER, ...);
   ORD(...)       = VAL(CARDINAL, ...);
   FLOAT(...)     = VAL(REAL, ...);
   LFLOAT(...)    = VAL(LONGREAL, ...);
   TRUNC(...)     = VAL(CARDINAL, ...);


Source:

  • Stony Brook Modula-2 documentation. Used with permission. Note: Stony-Brook M2 offers an extended syntax with features not described here. Stony Brook M2 users are encouraged to visit the Stony Brook website and to consult the Stony Brook help system.