OPTIONAL attribute
You can specify OPTIONAL as part of the parameter-descriptor list or as an attribute in the parameter declaration.
OPTIONAL arguments can be omitted in calls and function references by specifying an asterisk for the argument. An omitted item can be anywhere in the argument list, including at the end. However, the omitted item is counted as an argument. With its inclusion in an entry, the number of arguments must not exceed the maximum number allowed for the entry.
Using OPTIONAL and BYVALUE for the same item is not valid, unless the item is a LIMITED ENTRY.
The receiving procedure can use the OMITTED or PRESENT built-in function to determine whether an OPTIONAL parameter/argument was omitted in the invocation of the entry.
You can pass an omitted OPTIONAL parameter as an argument to an entry if the corresponding parameter in the declaration for that entry is also OPTIONAL.
If the final parameters in an ENTRY declaration are declared as OPTIONAL, the ENTRY can be invoked with those parameters completely omitted: it is not even necessary to specify the appropriate number of asterisks. For example, if an ENTRY is declared as having five parameters, of which the last two have the OPTIONAL attribute, it can be invoked with three, four, or five arguments.
You can omit such trailing OPTIONAL parameters both when the ENTRY invoked is explicitly declared and when the ENTRY invoked is a nested sub-procedure. Note also that unless the ENTRY has the OPTIONAL(ASSEMBLER) attribute, the generated code will supply null pointers for the omitted parameters.
Valid and invalid call statements
shows both valid and invalid CALL statements for the procedure
Vrtn.
Caller: proc;
dcl Vrtn entry (
fixed bin,
ptr optional,
float,
* optional);
/* The following calls are valid: */
call Vrtn(10, *, 15.5, 'abcd');
call Vrtn(10, *, 15.5, *);
call Vrtn(10, addr(x), 15.5, *);
call Vrtn(10, *, 15.5);
call Vrtn(10, addr(x), 15.5);
/* The following calls are invalid: */
call Vrtn(*, addr(x));
call Vrtn(10,addr(x));
call Vrtn(10);
call Vrtn;
end Caller;
Vrtn: proc (Fb, P, Fl, C1);
dcl Fb fixed bin,
P ptr optional,
Fl float,
C1 char(8) optional;
if ¬omitted(C1) then display (C1);
if ¬omitted(P) then P=P+10;
end;Vrtn determines whether OPTIONAL parameters are omitted
and takes the appropriate action.
