optional

declaration/definition

Syntax
argument-class argument-type argument-name optional (initial {initial-values})?


Purpose

Declares a function's argument as optional. An optional argument can be omitted from a call by omitting the preceding function argument separator together with the value of the argument.

The initial value of an optional function argument can be a dynamic value. The default initial value is calculated each time the function is called and the default value is needed. This means that default values can depend on global and constant values.

An argument that is declared optional can also be provided with a default; the default is specified as a shelf literal. Remainder arguments cannot be optional.

When an optional argument with no default value is omitted from a function call, it is illegal to access that argument from within the function in any way, except to test whether it was specified. (Whether an optional argument was specified in a call can be determined using the is specified argument test.) An optional argument can be passed to another function as an optional argument even if it was not specified in the function call .

In particular, for an argument whose value is not specified in the function call, these two statements can be made:

  • For an optional argument with a default, the argument takes on the default specified in the function definition following the keyword initial. Note that the argument still is not specified, even though it has a default.
  • For an optional argument with no default, the argument has no usable value (except that you can pass the argument on to a function that takes an optional argument). Any attempt to use the value in the function, other than testing it for is specified, is an error.

For example, the following function is defined with an optional argument:

  define function 
     increment    modifiable integer x 
               by value      integer y optional initial { 1 } 
  as
     set x to x + y

Related Syntax