Previous: , Up: Arguments   [Contents][Index]


6.4.1.2 Empty Arguments

You may specify no value for some or all of the arguments to a function or subroutine. What happens in that case depends on the routine or function. Some of the arguments to the routine may be required. If you do not specify a value for such an argument, then an error is generated.

For some of the arguments to a routine a default value may be specified inside the routine. In that case, if the user specifies no value for that argument, then the default value is used instead as if the user specified that value.

Default values can be assigned to arbitrary variables (including parameters of user-defined subroutines and functions) using either the default subroutine (default) or the defined function (defined). For instance,

  default,x,2
  if not(defined(x)) then x = 2

both assign the value 2 to variable x if it was undefined before. If x is a pointer (Pointers) or a parameter to a user-defined subroutine or function, then it is considered to be undefined if it is ultimately linked to a value that is undefined. Global variables (Global Variables) are treated in the same way.

Inside a user-defined subroutine or function the parameters of the routine must be distinguished from the other variables, because we must be able to tell the difference between the case where the user did not specify a value for the parameter and the case where the user did specify a value but the value happens to be undefined. The use of the defined function as above would return 0 in both of these cases.

DEFINED(x, /TARGET) returns 1 if x is a pointer, and if x is a parameter to a user-defined function or subroutine for which the user has specified a value (even if that value happens to be undefined).

Consider the following example:

  subr foo,x
    t,defined(x),defined(x,/argument)
    fie,x
  endsubr

  subr fie,a
    t,defined(a),defined(a,/argument)
  endsubr

If one executes foo,5 then one gets 1, 1, 1, 1 as result, because the parameters x of foo and a of fie have the value 5. If one executes foo,z where z is undefined, then one gets 0, 1, 0, 1, because a value was defined for the parameters, but that value is undefined. If one executes foo without any argument, then one gets 0, 0, 0, 0, because the user did not specify a value for any of the parameters.

Note that zeros are returned by fie in this case even though an argument to fie was specified in foo: the argument to fie in foo itself did not have a user-specified value, and this property is inherited by the parameter of fie.


Previous: , Up: Arguments   [Contents][Index]