Passing by Reference
Parameters are passed to IDL system and user-written procedures and
functions by value or by reference. It is important to recognize
the distinction between these two methods.
Suppose that the following statements occur within an IDL program
s=10
t=15
r=myfun(s,t)
IDL will create variables s and t with the assigned values. One can imagine that there are locations in memory that hold these variables. To be able to find the variables IDL has a second table that contains the name and location of each variable. When the function myfun(s,t) is called, IDL goes to the location table and finds the location (address) of each variable. It then provides those locations to the function as its arguments.
When the function is evaluated the system uses the location information to go and find its current value and other descriptive information. It then uses that information as required within the function program. It is possible to change the value of a parameter during evaluation of the function. When the function evaluation is completed the system writes the current value of each parameter back into its storage location. The calling program will then find that the value has been changed. A function program also returns a value to be assigned to a variable, such as r in the above example. Thus, information can be passed back from the function to the calling program either through the returned value or through changes made to the parameters.
Passing parameters by the above method is called passing by reference. This is used for all variables in IDL.
An example program that might make use of this method is given below.
FUNCTION switch,a,b
c=a
a=b
b=c
RETURN,c
END
The sequence of statements
s=10
t=15
r=switch(s,t)
will cause s to have the value 15, t to have the value 10 and r to
have the value 10.
A similar result could be obtained by writing SWITCH as a procedure. We'll call it SWAP.
PRO swap,a,b,c
c=a
a=b
b=c
END
The sequence of statements
s=10
t=15
swap,s,t,r
will cause s to have the value 15, t to have the value 10 and r to
have the value 10. An advantage of the procedure over the function is that
if you are not interested in r then the statement swap,s,t will work just
as well.
Passing by Value
Expressions, constants, system variables, and subscripted variable
references are passed by value.
This means that the actual value of any of these items, rather than the location in memory is passed to functions and procedures. The value that is passed is available to the computations within the called function or procedure.
An error will occur if a function or procedure attempts to change a parameter that received information through pass by value. For example consider a call to the procedure swap that is described above. A statement such as swap,3,5 would cause the procedure to attempt to replace the value of 3 with 5 and vice versa. This makes no sense, and is an error. Hence, one must not try to pass values to parameters that are changed.
If a and b are arrays then any element, such as a[3] or b[3:7], is passed
by value. However, the whole array is passed by reference. Thus, swap,a,b
would work but swap,a[3],b[5] would fail. This sometimes causes a problem
when you really do want to swap a[3] and b[5], but it is possible to work
around it with
s=a[3] & t=b[5]
swap,s,t
a[3]=s & b[5]=t
Hmmm. See why that works?
See Parameter Passing Mechanism in the
IDL online help for further discussion.