An ANYVAR parameter is similar to a VAR parameter in that
its actual parameter is passed by reference and must be a variable
access. If the routine changes the value of a formal ANYVAR parameter,
it changes the value of the actual parameter.
An ANYVAR parameter differs from a VAR parameter in that its
actual parameter can be of any type. HP Pascal treats the actual
parameter as if it were of the data type of the formal ANYVAR parameter.
This is implicit type coercion.
Example 1
$STANDARD_LEVEL 'HP_MODCAL'$ PROGRAM prog; TYPE type1 = ARRAY [1..10] OF integer; type2 = ARRAY [1..20] OF integer; type3 = ARRAY [1..11] OF real; VAR var1 : type1; var2 : type2; var3 : type3; PROCEDURE p ( VAR parm1 : type1; ANYVAR parm2 : type2); EXTERNAL; BEGIN p(var1, {legal} var1); {legal} p(var2, {illegal must be of type1} var2); {legal} p(var3, {illegal must be of type1} var3); {legal} END. |
The formal VAR parameter parm1
must have an actual parameter of type type1.
The formal ANYVAR parameter parm2
can have an actual parameter of any type.
The first call to procedure p passes
the variable var1
(a 10-element integer array) to parm2
(a 20-element integer array). This is legal because parm2
is an ANYVAR parameter; however, parm2[11]
through parm2[20]
are undefined. Accessing them causes unpredictable results.
The second call to p
passes the variable var2
to parm2. Both
are 20-element integer arrays. The procedure p
can access all 20 elements of parm2.
The third call to p
passes the variable var3
(an 11-element real array) to parm2
(a 20-element integer array). Although this is legal, p
must not try to access any of the nonexistent elements parm2[12]
through parm2[20].
The procedure p
treats the elements of parm2
as if they were integers (although the elements of var3
are real).
The implicit type coercion requires that the actual parameter
be aligned on a boundary that is the same or larger than the boundary
on which the formal parameter is aligned (for example, if the formal
parameter is 2-byte-aligned, the actual parameter can be 2-byte-aligned
or 4-byte-aligned, but it cannot be byte-aligned).
Example 2
PROGRAM prog; VAR c : PACKED ARRAY [1..2] OF char; j : shortint; i : integer; PROCEDURE show_anyvar_alignment (ANYVAR anyvar_parm : shortint); EXTERNAL; BEGIN show_anyvar_alignment(c); {illegal must be 2-byte-aligned} show_anyvar_alignment(j); {legal} show_anyvar_alignment(i); {legal references high-order 2 bytes} END. |
When HP Pascal passes an actual parameter to a formal ANYVAR
parameter, it also passes a hidden parameter. The hidden parameter
can be used to determine the size of the actual parameter. See “Hidden Parameters ” for more information.