The TEMPORARY routine is a simple way to use memory more efficiently. Statements of the form
create a temporary variable to hold the intermediate result a + 5. This temporary variable effectively doubles the amount of memory needed to store a (but only for the time necessary to do the operation). This is not necessary if we know that the expression a + 5 will be reassigned to a. In this case, a itself can be used as the temporary variable. Use TEMPORARY to indicate to IDL which variable can be used as the temporary variable:
Now, adding 5 will happen "in place" in the memory that holds a. Incidently, for operations like the above, an even more efficient way to perform this calculation is:
There are composite assignment forms like the above for all of IDL's binary operators.
On the other hand, TEMPORARY does not help for expressions where the variable to be reassigned to is used multiple times like
Neither a on the right-hand side of this statement can be marked temporary because marking a variable as temporary makes the variable undefined (until it is reassigned to again). In this case, if the a in a^3 was marked as temporary, then the a in a^2 would be undefined. (And vice versa.)
The argument of TEMPORARY must be a named variable to have an effect. So
will perform its arithmetic correctly, but will create a temporary variable in doing it.

May 16th, 2006 at 10:39 pm
I often see programmers using subscripted statements in their TEMPORARY expressions, such as
a = temporary(a[*, *, i])*5
Using TEMPORARY here doesn’t save anything since a subscripted array creates an expression, which is a copy of the referenced memory.
May 31st, 2006 at 2:54 pm
What about something like this:
a = sin(temporary(a))
I’d like to do the sine calculation in place, but wouldn’t
temporary(a)be passed by value to SIN, thereby creating a copy ofa?June 2nd, 2006 at 10:57 pm
Yes, it would. I changed my example to something more realistic.