Previous Application Programming: Creating Procedures and Functions Next

Supplying Values for Missing Arguments

The N_PARAMS function returns the number of positional arguments (not keyword arguments) present in a procedure or function call. A frequent use is to call N_PARAMS to determine if all arguments are present and if not, to supply default values for missing parameters. For example:

; Print values of XX and YY. If XX is omitted, print   
; values of YY versus element number.  
PRO XPRINT, XX, YY  
  
   ; Check number of arguments.  
   CASE N_PARAMS() OF   
  
     ; Single-argument case.  
     1: BEGIN   
  
       ; First argument is y values.  
       Y = XX  
  
       ; Create vector of subscript indices.  
       X = INDGEN(N_ELEMENTS(Y))  
  
       END  
  
     ; Two-argument case.  
     2: BEGIN  
  
       ; Copy parameters to local arguments.  
       Y = YY & X = XX  
  
       END  
  
     ; Print error message.  
     ELSE: MESSAGE, 'Wrong number of arguments'  
  
   ENDCASE  
  
   ; Remainder of procedure.  
   ...  
  
END  

  IDL Online Help (March 06, 2007)