Value versus Reference Parameters [ HP Pascal/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP Pascal/iX Programmer's Guide
Value versus Reference Parameters
The terms value and reference must be explained in terms of formal and
actual parameters. A formal parameter is defined in a routine header.
An actual parameter is passed in a call to a routine.
Example 1
PROGRAM prog;
VAR
a : integer;
PROCEDURE p (f : integer); {f is a formal parameter}
BEGIN
END;
BEGIN
p(a); {a is an actual parameter}
END;
A value parameter is passed by value; that is, the value of the actual
parameter is passed to the routine and assigned to the formal parameter.
If the routine changes the value of the formal parameter, it does not
change the value of the actual parameter. An actual value parameter can
be a constant, an expression, a variable, or a function result.
A reference parameter is passed by reference; that is, the address of the
actual parameter is passed to the routine and associated with the formal
parameter. If the routine changes the value of the formal parameter, it
changes the value of the actual parameter. An actual reference parameter
must be a variable access (a variable name or the name of a component of
an unpacked structure).
HP Pascal without system programming extensions has one kind of reference
parameter: VAR. For more information on VAR parameters, refer to the HP
Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual,
depending on your implementation.
HP Pascal with system programming extensions has two additional kinds of
reference parameters: ANYVAR and READONLY. An actual READONLY parameter
can be a constant, an expression, or a function result.
Example 2
PROGRAM prog;
VAR
a,b : integer;
PROCEDURE p ( x : integer; {x is a value parameter}
VAR y : integer); {y is a reference parameter}
BEGIN
x := x+1; {this does not change x's actual parameter}
y := y+1; {this does change y's actual parameter}
writeln(x); {this writes 41}
writeln(y); {this writes 61}
END;
BEGIN
a := 40;
b := 60;
p(a,b);
writeln(a); {this writes 40}
writeln(b); {this writes 61}
END.
Table 7-1 compares the four kinds of formal parameters.
Table 7-1. Comparison of Kinds of Formal Parameters
------------------------------------------------------------------------------------------------------
| | | | | |
| | | | | |
| | | | | |
| Kind of | | Actual | Actual | Routine Can Modify |
| Formal | STANDARD_LEVEL | Parameter Can | Parameter Is | |
| Parameters | | Be | Passed By | |
| | | | | |
------------------------------------------------------------------------------------------------------
| | | | | | |
| | | | | Parameter | Actual |
| | | | | | Parameter |
| | | | | | |
| Value | ANSI | Constant, | Value | Yes | No |
| | | expression | | | |
| | | variable, or | | | |
| | | function | | | |
| | | result | | | |
| | | | | | |
------------------------------------------------------------------------------------------------------
| | | | | | |
| Var | ANSI | Variable only | Reference | Yes | Yes |
| | | | | | |
------------------------------------------------------------------------------------------------------
| | | | | | |
| ANYVAR | HP_MODCAL | Variable only | Reference | Yes | Yes |
| | | | | | |
------------------------------------------------------------------------------------------------------
| | | | | | |
| READONLY | HP_MODCAL | Constant, | Reference | No | No |
| | | expression, | | | |
| | | variable, or | | | |
| | | function | | | |
| | | result | | | |
| | | | | | |
------------------------------------------------------------------------------------------------------
MPE/iX 5.0 Documentation