DEFAULT_PARMS [ HP Pascal/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP Pascal/iX Programmer's Guide
DEFAULT_PARMS
The DEFAULT_PARMS procedure option specifies default values to be
assigned to formal parameters when actual parameters are not passed to
them. If a nonextension parameter has a default value, its actual
parameter can be left out of the actual parameter list, and its default
value will be assigned to the formal parameter.
A default value must be a constant expression that is assignment
compatible with its parameter. The value nil is the only legal default
for a VAR, ANYVAR, function or procedure parameter.
Example
PROGRAM prog;
PROCEDURE p (a,b,c : integer)
OPTION DEFAULT_PARMS (b:=2,c:=3); {two have default values}
BEGIN
.
.
.
END;
BEGIN
p(10); {a:=10, b:=2 (default), c:=3 (default)}
p(10,20); {a:=10, b:=20, c:=3 (default)}
p(10,,30); {a:=10, b:=2 (default), c:=30}
p(); {illegal}
p(,20); {illegal}
END.
If an extension parameter has a default value, its actual parameter can
be left out of the middle or off the end of the actual parameter list.
If it is left out of the middle, its default value is assigned to the
formal parameter. If it is left off the end, no value is assigned to the
formal parameter.
Example
PROGRAM prog;
PROCEDURE p (a,b,c : integer)
OPTION EXTENSIBLE 0 {all parameters are extensible}
DEFAULT_PARMS (a:=1,b:=2,c:=3); {all have default values}
BEGIN
.
.
.
END;
BEGIN
p(9,,5); {a:=9, b:=2 (default), c:=5}
p(6,7); {a:=6, b:=7, no value assigned to c}
p(8); {a:=8, no value assigned to b or c}
p(,4,5); {a;=1 (default), b:=4, c:=5}
END.
Table 8-1 tells the value that is passed to a formal parameter, x,
when x is:
* Nonextension or extension.
* Its actual parameter is specified or not specified.
* It is before, the same as, or after the parameter n, where n is
the last parameter for which an actual parameter is specified.
Table 8-1. Values Passed to Formal Parameter x
----------------------------------------------------------------------------------------------------
| | | |
| Type of Parameter | Actual | Position of x informal parameter list p(..,n,..) |
| | Parameter | where n is the last actual parameter specified in |
| | for x is | the actual parameter list p(..,n) |
| | Specified | |
| | | |
----------------------------------------------------------------------------------------------------
| | | | | |
| | | x is before n: | x is n: | x is after n: |
| | | p(.x.,n,..) | p(..,x,..) | p(..,n,.x.) |
| | | | | |
----------------------------------------------------------------------------------------------------
| | | | | |
| Nonextension | Yes | Actual value | Actual value | Impossible |
| Parameter | | | | because x > n |
| | | | | |
----------------------------------------------------------------------------------------------------
| | | | | |
| Nonextension | No | Default value if | Impossible | Illegal unless |
| Parameter | | specified; error | because x=n | defaulted, then |
| | | otherwise | | defaulted value |
| | | | | |
----------------------------------------------------------------------------------------------------
| | | | | |
| Extension Parameter | Yes | Actual value | Actual value | Impossible |
| | | | | because x > n |
| | | | | |
----------------------------------------------------------------------------------------------------
| | | | | |
| Extension Parameter | No | Default value if | Impossible | No value |
| | | specified; error | because x=n | |
| | | otherwise | | |
| | | | | |
----------------------------------------------------------------------------------------------------
Haveoptvarparm Function
A routine can use the predefined function haveoptvarparm to determine
whether the value that it received for a formal reference parameter was
passed as an actual parameter or defaulted.
The predefined function haveoptvarparm returns true and false under these
conditions:
-------------------------------------------------------------------------------------
| | | |
| Function | Returns true | Returns false |
| | | |
-------------------------------------------------------------------------------------
| | | |
| haveoptvarparm(x) where x | If the routine was passed | If the routine was not |
| is a formal reference | an actual parameter for x | passed an actual |
| parameter of the routine | | parameter for x (in which |
| that called | | case, x assumes its |
| haveoptvarparm | | default value, nil) |
| | | |
-------------------------------------------------------------------------------------
Example
PROGRAM prog;
$STANDARD_LEVEL 'EXT_MODCAL'$
VAR
i : integer;
PROCEDURE p (VAR x,y : integer)
OPTION DEFAULT_PARMS (x := nil, y := nil);
VAR
b : boolean;
BEGIN
b := haveoptvarparm(x); {b := true for p(i)}
b := haveoptvarparm(y); {b := false for p(i)}
END;
BEGIN
p(i); {x=i, y=nil (default)}
END.
Table 8-2 tells the value of haveoptvarparm(x) when the formal
parameter x meets the following conditions:
* Nonextension or extension.
* Its actual parameter is specified or not specified.
* It is before, the same as, or after the parameter n, where n is
the last parameter for which an actual parameter is specified.
Table 8-2. Values Returned by Haveoptvarparm(x)
--------------------------------------------------------------------------------------------------
| Type of Parameter | Actual | Position of x in formal parameter list p(..,n,..) |
| | Parameter | where n is the last actual parameter specified in |
| | for x is | the actual parameter list p(..,n) |
| | Specified | |
--------------------------------------------------------------------------------------------------
| | | x is before n: | x is n: | x is after n: |
| | | p(.x.,n,..) | p(..,x,..) | p(..,n,.x.) |
--------------------------------------------------------------------------------------------------
| Nonextension | Yes | true | true | Impossible x > n |
| Parameter | | | | |
--------------------------------------------------------------------------------------------------
| Nonextension | No | false | Impossible, | false |
| Parameter | | | because x=n | |
--------------------------------------------------------------------------------------------------
- Extension Parameter - Yes - true - true - Impossible, x > n -
--------------------------------------------------------------------------------------------------
| Extension Parameter | No | false | Impossible, | false |
| | | | because x=n | |
--------------------------------------------------------------------------------------------------
Haveextension Function
With the DEFAULT_PARMS procedure option, the predefined function
haveextension returns true and false under these conditions:
-------------------------------------------------------------------------------------
- Function - Returns true - Returns false -
-------------------------------------------------------------------------------------
| haveextension(x) where x | If the routine was passed | If the routine was not |
| is a formal parameter of | an actual parameter for | passed an actual |
| the routine that called | x, or if DEFAULT_PARMS | parameter for x, and no |
| haveextension. | specified a default for | default was specified for |
| | x. | x with DEFAULT_PARMS. |
-------------------------------------------------------------------------------------
Example
PROGRAM prog;
$STANDARD_LEVEL 'EXT_MODCAL'$
PROCEDURE p (a,b,c : integer)
OPTION EXTENSIBLE 2
DEFAULT_PARMS (b:=2);
BEGIN
END;
BEGIN {haveextension(b)} {haveextension(c)}
p(10,20); {true} {false}
p(10,20,30); {true} {true}
p(10); {true} {false}
END.
Table 8-3 tells the value of haveextension(x) when the formal
parameter x is:
* Nonextension or extension.
* Its actual parameter is specified or not specified.
* It is before, the same as, or after the parameter n, where n is
the last parameter for which an actual parameter is specified.
Table 8-3. Values Returned by Haveextension(x)
-----------------------------------------------------------------------------------------------------
| | | |
| | Actual | Position of x informal parameter list p(..,n,..) |
| | Parameter | where n is the last actual parameter specified in the |
| Type of Parameter | for x is | actual parameter list p(..,n) |
| | Specified | |
| | | |
-----------------------------------------------------------------------------------------------------
| | | | | |
| | | x is before n: | x is n: | x is after n: |
| | | p(.x.,n,..) | p(..,x,..) | p(..,n,.x.) |
| | | | | |
| Nonextension | Yes | Calling haveextension(x) causes a compile-time error. |
| Parameter | No | |
| | | |
-----------------------------------------------------------------------------------------------------
| | | | | |
| Extension Parameter | Yes | true | true | Impossible |
| | | | | |
-----------------------------------------------------------------------------------------------------
| | | | | |
| | No | true | Impossible, | false |
| | | | because x=n | |
| | | | | |
-----------------------------------------------------------------------------------------------------
MPE/iX 5.0 Documentation