Re: static variables [message #7119 is a reply to message #2690] |
Mon, 30 September 1996 00:00   |
Eric Deutsch
Messages: 11 Registered: May 1995
|
Junior Member |
|
|
Esfandiar Bandari wrote:
>
> Hi:
> I am newcomer to idl and this news group! I was wondering if
> there is any clean way of having static variables inside files or
> functions (as in C or C++). These are variables that keep their value
> from function call to function call and are local to the function.
I think your only option is to use common blocks. e.g.:
function calc1,var1,var2
common COM_CALC1,var3,var4
if (n_elements(var3) eq 0) then var3=0.0
if (n_elements(var4) eq 0) then var4=50.0
if (n_elements(var2) ne 0) then var4=var2
print,var3
var3=var3+1
return,var1*var2*var3
end
If calc1 is the only function that ever uses the common block COM_CALC1
then, var3,var4 work essentially like static variables. Note that there
is
the option of other functions/procedures also using this common
block which then makes var3,var4 a little bit more like C global
variables.
Also, IDL won't let you stuff var1,var2 into a common block. In the
needlessly-complicated example above, the function automatically starts
counting the number of times it gets called in an IDL session, and var4
can be specified in the function call, or if left unspecified in the
function call, takes on the value from the previous call (or the default
of 50.0)
hope this helps,
Eric
--
------------------------------------------------------------ ----------------
Eric Deutsch email:
deutsch@astro.washington.edu
Department of Astronomy Voice: (206)
616-2788
University of Washington FAX: (206)
685-0403
Box 351580 WWW:
http://www.astro.washington.edu/deutsch/
Seattle, WA 98195-1580 Physics/Astronomy Bldg., Room
B356F
|
|
|