HP Pascal/iX Programmer's Guide (31502-90023)
7- 12
Example 3
This program uses functions whose return types are function and procedure
types to assign values to routine variables. The comments tell you which
assignments are illegal and why.
$STANDARD_LEVEL 'HP_MODCAL'$
PROGRAM proc;
TYPE
proctype1 = PROCEDURE (x : integer);
proctype2 = PROCEDURE (x,y : integer);
functype1 = FUNCTION (y : real) : integer;
functype2 = FUNCTION (y : real) : real;
VAR
procvar : proctype1;
funcvar : functype1;
FUNCTION returnproc1 (z : integer) : proctype1; EXTERNAL;
FUNCTION returnproc2 (z : integer) : proctype2; EXTERNAL;
FUNCTION returnfunc1 : functype1; EXTERNAL;
FUNCTION returnfunc2 : functype2; EXTERNAL;
BEGIN
procvar := returnproc1(1);
procvar := returnproc2(2); {illegal -- function returns wrong type}
funcvar := returnfunc1;
funcvar := returnfunc2; {illegal -- function returns wrong type}
END.
Example 4
Undefined routine variables are undetectable, and cause unpredictable
results. The following program avoids problems caused by such undefined
variables by assigning the value
nil
to those variables.
$STANDARD_LEVEL 'EXT_MODCAL'$
PROGRAM prog (input,output);
VAR
i,j : integer;
procvar1 : PROCEDURE (a,b : integer);
procvar2 : PROCEDURE (VAR c,d : integer);
PROCEDURE alpha (x,y: integer); EXTERNAL;
PROCEDURE beta (x,y: integer); EXTERNAL;
PROCEDURE gamma (VAR x,y: integer); EXTERNAL;
PROCEDURE delta (VAR x,y: integer); EXTERNAL;
BEGIN
read(i,j);
{initialize variables of procedure type}
procvar1 := nil;
procvar2 := nil;
{If -100 <= i <= -1, procvar1 is alpha;
if 0 <= i <= 100, procvar1 is beta}
IF (i IN [-100..-1] THEN procvar1 := addr(alpha)
ELSE IF i IN [0..100] THEN procvar1 := addr(beta);
{If -10 <= j <= -1, procvar2 is gamma;
if 0 <= j <= 10, procvar2 is delta}