Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Pascal/HP-UX Programmer's Guide > Chapter 7 Parameters

Value versus Reference Parameters

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

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 “Comparison of Kinds of Formal Parameters” compares the four kinds of formal parameters.

Table 7-1 Comparison of Kinds of Formal Parameters

Kind of Formal Parameters

STANDARD_LEVEL

Actual Parameter Can Be

Actual Parameter Is Passed By

Routine Can Modify

Parameter

Actual Parameter

Value

ANSI

Constant, expression variable, or function result

Value

Yes

No

Var

ANSI

Variable only

Reference

Yes

Yes

ANYVAR

HP_MODCAL

Variable only

Reference

Yes

Yes

READONLY

HP_MODCAL

Constant, expression, variable, or function result

Reference

No

No

 

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.