Previous Topic Next topic Print topic


OPTIONAL

The OPTIONAL attribute can be specified as part of the parameter-descriptor list or as an attribute in a parameter declaration.

OPTIONAL

Use an asterisk (*) to omit OPTIONAL arguments in calls and function references. Omitted items can occur anywhere in the argument list. Each omitted item is counted as an argument. The number of arguments must not exceed the maximum number allowed for the entry.

Use the OMITTED or PRESENT built-in functions in the receiving procedure to determine if an OPTIONAL parameter/argument is omitted or present in the entry invocation.

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.

Any final parameters in an ENTRY declaration that are declared as OPTIONAL, can be omitted in the ENTRY invocation. You do not have to specify the appropriate number of asterisks when omitting these parameters in the invocation. So for example, if declaring an ENTRY with six parameters where the last three are declared as OPTIONAL, then you can invoke it with three, four, five, or six arguments.

Example

This example cycles through a list of calls that include some optional parameters. The OMITTED built-in is used to determine if the OPTIONAL parameters are supplied on each call and prints the results.

pgm: proc options (main);

dcl subr entry ( fixed bin, char(1) optional, float, char(40) optional);
dcl c char(1);

call subr(99, *, 3.14, 'OPTIONAL attribute & OMITTED built-in');
call subr(99, *, 3.14, *);
call subr(99, c, 3.14);
call subr(99, *, 3.14, *);
call subr(99, c, 3.14, 'Done.');

end pgm;

subr: proc (i, c1, f, str);

dcl i    fixed bin,
    c1   char(1) optional,
    f    float bin,
    str  char(40) optional;

if omitted(str) then
    put skip list('Arg str Omitted.');
else
    put skip data(str);

if omitted(c1) then
    put skip list('Arg c1 Omitted.');
else
    put skip list('Arg c1 not Omitted.');

put skip list ('--------');

end subr;

When executed, this code example prints:

STR='OPTIONAL attribute & OMITTED built-in   ' ;
Arg c1 Omitted.
--------
Arg str Omitted.
Arg c1 Omitted.
--------
Arg str Omitted.
Arg c1 not Omitted.
--------
Arg str Omitted.
Arg c1 Omitted.
--------
STR='Done.                                   ' ;
Arg c1 not Omitted.
--------
Previous Topic Next topic Print topic