Determining if a Keyword is Set
The previous function example (in Using Keyword Parameters) uses the system function KEYWORD_SET to determine if a keyword parameter has been passed and if it is nonzero. This is similar to using the condition:
IF N_ELEMENTS(P) NE 0 THEN IF P THEN ... ...
to test if keywords that have a true/false value are both present and true. The N_ELEMENTS function returns the number of elements contained in any expression or variable. Scalars always have one element. The N_ELEMENTS function returns zero if its parameter is an undefined variable. The result is always a longword scalar. The following example determines if a variable is defined using N_ELEMENTS. It sets the variable abc
to zero if it is undefined; otherwise, the variable is not changed.
IF N_ELEMENTS(abc) EQ 0 THEN abc = 0
The KEYWORD_SET function returns a 1 (true), if its parameter is defined and nonzero; otherwise, it returns zero (false). For example, assume that a procedure is written which performs and returns the result of a computation. If the keyword PLOT is present and nonzero, the procedure also plots its result as follows:
; Procedure definition.
PRO XYZ, result, PLOT = plot
; Compute result.
...
; Plot result if keyword parameter is set.
IF KEYWORD_SET(PLOT) THEN PLOT, result
END
A call to this procedure that produces a plot is shown in the following statement.
XYZ, R, /PLOT