Standard proceduresThe 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. ABSPROCEDURE 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)) CAPPROCEDURE 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. CHRPROCEDURE 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. DECPROCEDURE 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. DISPOSEPROCEDURE 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. EXCLPROCEDURE 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. FLOATPROCEDURE FLOAT(num : AnyIntegerType) : REAL; Converts the integer number num to REAL. num can be any signed or unsigned integer type. LENGTHPROCEDURE 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. LFLOATPROCEDURE FLOAT(num : AnyIntegerType) : LONGREAL; Converts the integer number num to LONGREAL. num can be any signed or unsigned integer type. HALTPROCEDURE 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. HIGHPROCEDURE 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); 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. INCLPROCEDURE 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. INTPROCEDURE 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. MAXPROCEDURE 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. MINPROCEDURE 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. NEWPROCEDURE NEW(VAR P : PointerType {;tag : tagtype}); PROCEDURE NEW(VAR P : DynamicArrayType; 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); 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. ODDPROCEDURE ODD(Val : ValType) : BOOLEAN; Returns TRUE if Val is ODD and FALSE otherwise. Val can be any signed or unsigned integer type. ORDPROCEDURE 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. REPROCEDURE RE(Val : COMPLEX) : REAL; PROCEDURE RE(Val : LONGCOMPLEX) : LONGREAL; Returns the real part of a complex type. IMPROCEDURE IM(Val : COMPLEX) : REAL; PROCEDURE IM(Val : LONGCOMPLEX) : LONGREAL; Returns the imaginary part of a complex type. CMPLXPROCEDURE 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. SIZEPROCEDURE 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. TRUNCPROCEDURE 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. VALPROCEDURE 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.
The following are TRUE: INT(...) = VAL(INTEGER, ...); Source:
|