Ch 8. Procedures and Functions [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation
HP Pascal/iX Reference Manual
Chapter 8 Procedures and Functions
When a procedure or function is declared, the heading may optionally
include a list of parameters. This list is called the formal parameter
list. A procedure statement or function call in the body of a block
provides the matching actual parameters that correspond by their order in
the list. The four kinds of formal parameters are value, reference,
functional, and procedural parameters. Value parameters are identifiers
followed by a colon (: ) and a type identifier or a conformant array
schema. Reference parameters are declared like value parameters, but are
preceded by the reserved word VAR. Functional or procedural parameters
are function or procedure headings.
The four types of formal parameters may be repeated and intermixed.
Several identifiers may appear separated by commas. These identifiers
then represent formal reference or value parameters of the same type,
even if the type is a conformant array schema.
A formal value parameter appears as a local variable during execution of
the procedure or function. It receives its initial value from the
matching actual parameter. Modification of the formal parameter cannot
affect the actual parameter which may be an expression. The actual
parameter must be assignment compatible with the formal parameter or, in
the case of a conformant array parameter, must conform with the formal
parameter.
A formal reference parameter represents the actual parameter during
execution of the procedure. Any changes in the value of the formal
reference parameter alters the value of the actual parameter, which must
be a variable access. The actual parameter must have a type identical
with the formal parameter or conform with the formal parameter, in the
case of a conformant array schema.
When a conformant array schema is specified, the value of the upper bound
and the value of the lower bound identifiers in the schema vary according
to the actual bounds of the array passed as the actual parameter. They
can be accessed as value parameters in the procedure, except their values
cannot be changed. Their names have the same scope as a parameter. The
type of the actual parameters must be conformable with the conformant
array schema. The formal parameters have a type that is distinct from
any other type. This means that the actual parameters are not assignable
to any other variable or parameter except those of the same type. The
type cannot be a PAC type since the lower bound cannot be fixed as one.
This makes passing string literals as actual conformant array parameters
an error in ISO Pascal. HP Pascal is extended to allow the passing of
string literals as parameters. However, a conformant array cannot be
manipulated as a string.
An actual conformant array parameter can be passed as a reference
conformant array parameter, but not as a value parameter of any kind.
A formal procedural or functional parameter is a synonym for the actual
procedure or function parameter. The parameter lists, if any, of the
actual and formal procedural or functional parameters must be congruent.
Two formal parameter lists are congruent if they contain an equal number
of parameters, and the parameters in corresponding positions are
equivalent. Two parameters are equivalent if any of the following
conditions are true:
* They are both value parameters of the identical type.
* They are both reference parameters of the identical type.
* They are both procedural parameters with congruent parameter
lists.
* They are both functional parameters with congruent parameter lists
and identical result types.
* They are both either value conformant array specifications or both
reference conformant array specifications, and in both cases, the
conformant array specifications contain the same number of
parameters and equivalent conformant array schemas. Two
conformant array schemas are equivalent if all of the following
statements are true:
* The ordinal type identifier in each corresponding index
type specification denotes the same type.
* Either the component conformant array schemas of the
conformant array schemas are equivalent, or the type
identifiers of the conformant array schemas denote the same
type.
* Either both conformant array schemas are packed or both are
unpacked.
Syntax
Formal_parameter_list:
Example
PROGRAM show_formparm (input);
VAR
test: boolean;
FUNCTION chek1 (x, y, z: real): Boolean;
BEGIN
{ Perform some type of validity check on x, y, z }
{ and return appropriate value. }
END;
FUNCTION chek2 (x, y, z: real): Boolean;
BEGIN
{ Perform an alternate validity check on x, y, z }
{ and return appropriate value. }
END;
PROCEDURE read_data (FUNCTION check (a, b, c: real): Boolean);
VAR p, q, r: real;
BEGIN
{ read and validate data }
readln (p, q, r);
IF check (p, q, r) THEN ...
END;
BEGIN {show_formparm}
.
IF test THEN read_data (chek1)
ELSE read_data (chek2);
.
END.
PROGRAM show_varparm(output);
VAR
i,j : integer;
PROCEDURE fix(VAR a : integer; b : integer);
BEGIN
a := b; { i is passed by reference; it will return equal to 42.}
b := 0; { j is passed by value; this assignment will }
{ not change the value of j in the main program. }
END;
BEGIN { show_varparm }
i:= 0;
j:= 42;
fix(i,j);
IF i = j THEN writeln('They both = 42');
END.
PROGRAM show_conformantparm;
CONST
First=1;
Last=10;
TYPE
inxtype=1..100;
arr1=ARRAY[First..Last] of Integer;
arr2=ARRAY[First..2*Last] of Integer;
VAR
a1,a2,a3:arr1;
b1,b2,b3:arr2;
PROCEDURE ADD_Array(
VAR Result:ARRAY[L..U:inxtype] OF INTEGER;
P1,P2:ARRAY[L1..U1:inxtype] OF INTEGER
);
VAR
inx:inxtype;
BEGIN { ADD_Array }
IF (L=L1) AND (U=U1) THEN
FOR inx:=L TO U DO
Result[inx]:=P1[inx]+P2[inx]
ELSE
{ handle the error }
END; { Add_Array }
BEGIN { Show_ConformantParm }
{ Initialize values for a1,a2,b1,b2 }
{ ADD_Array can be used for arrays of type arr1 and arr2
because they conform to each other.}
ADD_Array(a3,a1,a2);
ADD_Array(b3,b1,b2);
END. { Show_ConformantParm }
MPE/iX 5.0 Documentation