Calling Mechanism

When a user-written procedure or function is called, the following actions occur:

  1. All of the actual arguments in the user-procedure call list are evaluated and saved in temporary locations.
  2. The actual parameters that were saved are substituted for the formal parameters given in the definition of the called procedure. All other variables local to the called procedure are set to undefined.
  3. The function or procedure is executed until a RETURN or RETALL statement is encountered. Procedures also can return on an END statement. The result of a user-written function is passed back to the caller by specifying it as the value of a RETURN statement. RETURN statements in procedures cannot specify a return value.
  4. All local variables in the procedure, those variables that are neither parameters nor common variables, are deleted.
  5. The new values of the parameters that were passed by reference are copied back into the corresponding variables. Actual parameters that were passed by value are deleted.
  6. Control resumes in the calling procedure after the procedure call statement or function reference.

Recursion

Recursion (i.e., a program calling itself) is supported for both procedures and functions.

Example

Here is an example of an IDL procedure that reads and plots the next vector from a file. This example illustrates using common variables to store values between calls, as local parameters are destroyed on exit. It assumes that the file containing the data is open on logical unit 1 and that the file contains a number of 512-element, floating-point vectors.

; Read and plot the next record from file 1. If RECNO is specified,  
; set the current record to its value and plot it.  
PRO NXT, recno 
 
   ; Save previous record number. 
   COMMON NXT_COM, lastrec 
 
   ; Set record number if parameter is present. 
   IF N_PARAMS(0) GE 1 THEN lastrec = recno 
 
   ; Define LASTREC if this is first call. 
   IF N_ELEMENTS(lastrec) LE 0 THEN lastrec = 0 
 
   ; Define file structure. 
   AA = ASSOC(1, FLTARR(512)) 
 
   ; Read and plot record. 
   PLOT, AA[lastrec] 
 
   ; Increment record for next time. 
   lastrec = lastrec + 1 
 
END 

Once the user has opened the file, typing NXT will read and plot the next record. Typing NXT, N will read and plot record number N.