Procedures and Functions [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation
HP Pascal/iX Reference Manual
Procedures and Functions
The system programming extensions define two new formal parameter
mechanisms in addition to Pascal value and VAR formal parameters. These
mechanisms are ANYVAR and READONLY.
The system programming extensions also define an extension to the
procedure and function header syntax for specifying additional attributes
of a procedure or function. This extension is routine options.
Formal Parameters
The reserved words ANYVAR and READONLY can syntactically replace the
reserved word VAR in a formal parameter list specification.
ANYVAR.
This formal parameter mechanism implicitly type coerces the actual
parameter to the type of the formal parameter.
A formal ANYVAR parameter represents the actual parameter during
execution of the procedure. Any changes in the value of the formal
ANYVAR parameter alters the value of the actual parameter. Therefore, it
must be a variable-access parameter. The actual parameter may have any
type. The formal-ANYVAR parameter, however, is treated within the body
of the procedure as a variable of the type specified in its definition.
An additional hidden parameter is passed along with each actual parameter
passed to a formal ANYVAR parameter. This hidden parameter is the length
in bytes of the actual parameter. This size value can be accessed
through the use of the predefined functions sizeof and bitsizeof. This
additional size parameter is not passed when the routine option
UNCHECKABLE_ANYVAR is used.
This implicit reference type coercion is independent of the level of type
coercion selected when the actual parameter is used.
Example
TYPE
byte = 0..255;
byte_array = PACKED ARRAY [1..max_bound] OF byte;
VAR
int : integer;
rec : record_type;
PROCEDURE zero_bytes( ANYVAR arr : byte_array );
VAR
i : 0..max_bound;
limit : 1..max_bound;
BEGIN
IF (sizeof(arr) > max_bound) THEN
limit := max_bound
ELSE
limit := sizeof(arr);
FOR i := 1 TO limit DO
arr[i] := 0;
END;
BEGIN
zero_bytes( int );
zero_bytes( rec );
END;
READONLY.
This formal parameter mechanism protects the actual parameter from
modification within the procedure or function.
A formal READONLY parameter may not be:
* The target of an assignment statement.
* Passed as an argument to a VAR or ANYVAR parameter.
* Passed as an argument to the addr predefined function.
* Passed as an argument to any predefined routine that modifies that
argument.
In this way, modification of a variable passed as a READONLY parameter is
an error between the call to and return from the procedure or function by
modifying the formal parameter itself.
The actual parameter is passed by reference. If the actual parameter is
an expression or a constant, then a reference to a copy of the value is
passed.
Example
PROCEDURE proc( READONLY parm : integer );
VAR
pint : ^ integer;
PROCEDURE procx( VAR i : integer );
external;
BEGIN
...
parm := 0; { illegal : cannot assign to a READONLY }
procx( parm ); { illegal : cannot pass to a VAR parameter }
pint := addr( parm ); { illegal : cannot take its address }
...
END;
The above example creates detected errors.
NOTE The mechanism does not detect a modification of a READONLY
parameter by another reference parameter or an uplevel reference.
The results of such a modification are unpredictable.
Example
PROCEDURE proc1;
VAR
j : integer;
PROCEDURE proc2 ( READONLY j : integer
VAR m : integer );
BEGIN
j := 0; { modification by an uplevel reference }
m := 0; { modification by another reference parameter }
END;
BEGIN
proc2 ( j,j );
END;
The above example creates undetected errors.
Routine Options
The routine options specify additional attributes of a procedure or
function. The routine options follow the parameter list in the
declaration of a procedure or function header. $STANDARD_LEVEL
'EXT_MODCAL'$ must be specified when using routine options.
Syntax
The option-specification for each option is described in the following
pages.
For forward and external declarations of routines, the options specified
on the forward or external routine declaration must match the options on
the formal declaration.
DEFAULT_PARMS.
Normally, all parameters appearing in a formal parameter list must be
present in every corresponding actual parameter list.
The routine option DEFAULT_PARMS allows parameters to be omitted from the
actual parameter list. The option specifies which parameters may be
omitted, and the default values that the omitted parameters will assume.
Syntax
The expression supplied in the DEFAULT_PARMS option must be assignment
compatible with the corresponding formal parameter type. The expression
must also be a constant expression. The only default value permitted for
VAR, ANYVAR, and PROCEDURE/FUNCTION parameters is NIL.
Because defaulted reference parameters (VAR, ANYVAR, PROCEDURE/ FUNCTION
parameters) cannot be examined (their value is NIL, which cannot be
'dereferenced'), the predefined function haveoptvarparm can be used to
determine if a reference parameter was supplied by the caller. See the
section "Predefined Routines" for more information.
Example
PROCEDURE proc( i : integer )
OPTION DEFAULT_PARMS( i := -1 );
BEGIN
...
END;
...
proc( 1 ); { value of parameter is 1 }
proc( ); { value of parameter defaulted to -1 }
proc; { value of parameter defaulted to -1 }
See the HP Pascal/iX Programmer's Guide or the HP Pascal/HP-UX
Programmer's Guide, depending on your implementation, for more details on
OPTION DEFAULT_PARMS.
EXTENSIBLE.
Normally, all parameters appearing in a formal parameter list must be
present in a corresponding actual parameter list.
The routine option EXTENSIBLE allows parameters to be omitted from the
end of an actual parameter list. The option specifies the number of
non-extension parameters (those that must be supplied) in the actual
parameter list. The remaining trailing parameters may be omitted.
Syntax
Note that if a particular extension parameter is supplied in an actual
parameter list, then all EXTENSIBLE (and non-defaulted) parameters to the
left of the supplied parameter must also be supplied.
It is an error to access a formal parameter whose corresponding actual
parameter was not passed. An EXTENSIBLE parameter list, therefore, is
always passed with a hidden parameter describing the number of parameters
actually passed. The predefined function haveextension can be used to
determine if an EXTENSIBLE parameter is present. See the section
"Predefined Routines" for more details.
Example
PROCEDURE proc( i,j : integer )
OPTION EXTENSIBLE 0;
BEGIN
...
END;
...
proc; { both parameters not supplied }
proc( 1 ); { second parameter not supplied }
proc( 1,2 ); { both parameters passed }
proc( ); { illegal: implies a defaulted parameter }
proc( ,2 ); { illegal: only trailing parameters can be omitted }
proc( 1, ); { illegal: implies a defaulted second parameter }
Refer to the HP Pascal/iX Programmer's Guide or the HP Pascal/HP-UX
Programmer's Guide, depending on your implementation, for more
information on OPTION EXTENSIBLE.
INLINE.
The option INLINE specifies that the code for a procedure or function be
expanded in line wherever it is invoked. This expansion removes most
procedure call overhead and increases the amount of object code
generated. Value parameters work the same with INLINE, that is, an
assignment to a value parameter inside an inlined routine does not result
in the modification of the actual parameter.
INLINE procedures and functions cannot invoke themselves or any other
mutually recursive inline procedures or functions. The body of a
procedure or function must be supplied when INLINE is used.
Syntax
Example
PROCEDURE proc( x,y : integer ) OPTION INLINE;
...
BEGIN
...
END;
For more information about INLINE, refer to the HP Pascal/iX Programmer's
Guide or the HP Pascal/HP-UX Programmer's Guide, depending on your
implementation.
UNCHECKABLE_ANYVAR.
By default, every ANYVAR parameter is accompanied by a hidden size
parameter that indicates the size of the actual parameter. The purpose
of this parameter is to allow the routine with the formal ANYVAR
parameter to verify that a reference to the formal parameter is within
the bounds of the actual parameter by predefined size functions such as
sizeof.
Syntax
The option UNCHECKABLE_ANYVAR specifies that the hidden size parameter is
not passed by the caller and is not expected by the callee. Its primary
use is to interface with non-Pascal procedures and functions that do not
support the hidden size parameter.
A routine with the option UNCHECKABLE_ANYVAR must have at least one
ANYVAR parameter in its formal parameter list.
Calling the predefined size functions sizeof or bitsizeof for a formal
ANYVAR parameter with option UNCHECKABLE_ANYVAR, returns the size of the
formal parameter as opposed to the size of the actual parameter.
Example
PROCEDURE proc( ANYVAR arr : array_type )
OPTION UNCHECKABLE_ANYVAR;
BEGIN
END;
The routine above can be called from languages that do not support the
hidden size parameter because it has been declared with the option
UNCHECKABLE_ANYVAR.
UNRESOLVED.
Procedure option UNRESOLVED denotes a procedure or function that is left
unresolved by both the linker and the loader. The resolution of the
symbolic name to its reference part is delayed until the procedure or
function is used.
The suggested way to use this kind of procedure or function is to use the
predefined function addr to determine if it can be resolved. NIL is
returned if it cannot. This procedure option can be specified only on
level one procedures or functions.
NOTE On implementations that do not support dynamic loading, taking the
address of an unresolved routine always produces NIL, while calling
an unresolved routine is an error.
A procedure or function declared with option UNRESOLVED must not have a
body, and must be declared with the directive EXTERNAL.
Syntax
Example
PROCEDURE product_x
OPTION UNRESOLVED; external;
BEGIN
...
IF (addr( product_x ) <> nil) THEN { }
...
END;
The code above performs a check at run time for the existence of a
hypothetical product that provides the entry point product_x. If the
product does not exist at run time, and, therefore, does not have any of
its entry points installed, then the predefined function addr returns
NIL.
MPE/iX 5.0 Documentation