Programmer Guide > Writing Procedures and Functions > Procedure or Function Calling Mechanism
  

Procedure or Function Calling Mechanism
When a user-written procedure or function is called, the following actions take place:
*All of the actual arguments in the user procedure call list are evaluated and saved in a temporary location.
*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.
*The procedure is executed until a RETURN or RETALL statement is encountered. The result of a user-written function is passed back to the caller by specifying it as the parameter of a RETURN statement. RETURN statements in procedures may not have parameters.
*All local variables in the procedure (i.e., those variables that are neither parameters nor common variables) are deleted.
*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.
*Control resumes in the calling procedure after the procedure call statement or function reference.
Recursion
Recursion is supported with both procedures and functions.
Example Using Variables in Common Blocks
Here is an example of a procedure that reads and plots the next vector from a file. This example illustrates how to use 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.
PRO NXT, Recno
; Read and plot the next record from file 1. If Recno is speci
; fied, set the current record to its value and plot it.
COMMON Nxt_Com, Lastrec
; Save previous record number.
IF N_PARAMS(0) GE 1 THEN Lastrec = Recno 
; Set record number if parameter is present.
IF N_ELEMENTS(Lastrec) LE 0 THEN Lastrec = 0
;       Define Lastrec if this is first call.
AA = ASSOC(1, FLTARR(512))
; Define file structure.
PLOT, AA(Lastrec)
; Read record and plot it.
Lastrec = Lastrec + 1
; Increment record for next time.
RETURN
; All finished.
END
Once you have opened the file, typing NXT will read and plot the next record. Typing NXT, n will read and plot record number n.

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.