High procedure function (The GNU Modula-2 Compiler)

### 2.5 Behavior of the high procedure function ¶

This section describes the behavior of the standard procedure function `HIGH` and it includes a table of parameters with the expected return result. The standard procedure function will return the last accessible indice of an `ARRAY`. If the parameter to `HIGH` is a static array then the result will be a `CARDINAL` value matching the upper bound in the `ARRAY` declaration.

The section also describes the behavior of a string literal actual parameter and how it relates to `HIGH`. The PIM2, PIM3, PIM4 and ISO standard is silent on the issue of whether a `nul` is present in an `ARRAY` `OF` `CHAR` actual parameter.

If the first parameter to `HIGH` is an unbounded `ARRAY` the return value from `HIGH` will be the last accessible element in the array. If a constant string literal is passed as an actual parameter then it will be `nul` terminated. The table and example code below describe the effect of passing an actual parameter and the expected `HIGH` value.

    MODULE example1 ;

    PROCEDURE test (a: ARRAY OF CHAR) ;
    VAR
       x: CARDINAL ;
    BEGIN
       x := HIGH (a) ;
       ...
    END test ;

    BEGIN
       test ('') ;
       test ('1') ;
       test ('12') ;
       test ('123') ;
    END example1.

    Actual parameter | HIGH (a) | a[HIGH (a)] = nul
    ===============================================
     ''              | 0        | TRUE
     '1'             | 1        | TRUE
     '12'            | 2        | TRUE
     '123'           | 3        | TRUE

A constant string literal will be passed to an `ARRAY` `OF` `CHAR` with an appended `nul` `CHAR`. Thus if the constant string literal `''` is passed as an actual parameter (in example1) then the result from `HIGH(a)` will be `0`.

    MODULE example2 ;

    PROCEDURE test (a: ARRAY OF CHAR) ;
    VAR
       x: CARDINAL ;
    BEGIN
       x := HIGH (a) ;
       ...
    END test ;

    VAR
       str0: ARRAY [0..0] OF CHAR ;
       str1: ARRAY [0..1] OF CHAR ;
       str2: ARRAY [0..2] OF CHAR ;
       str3: ARRAY [0..3] OF CHAR ;
    BEGIN
       str0 := 'a' ;   (* No room for the nul terminator.  *)
       test (str0) ;
       str1 := 'ab' ;  (* No room for the nul terminator.  *)
       test (str1) ;
       str2 := 'ab' ;  (* Terminated with a nul.  *)
       test (str2) ;
       str2 := 'abc' ; (* Terminated with a nul.  *)
       test (str3) ;
    END example2.

    Actual parameter | HIGH (a) | a[HIGH (a)] = nul
    ===============================================
     str0            | 0        | FALSE
     str1            | 1        | FALSE
     atr2            | 2        | TRUE
     str3            | 3        | TRUE