A strange behaviour of parameters passing [message #15630] |
Thu, 03 June 1999 00:00  |
Daniel SAGE
Messages: 10 Registered: April 1998
|
Junior Member |
|
|
Hi,
Who can explain me the strange behaviour when I passed my parameters ?
I create a small and stupid example to see the disfunctionning of IDL.
Why the first call of the procedure 'one' returns 1 and the second call
return 2 ? According the documentation of IDL, the second calling is
correct but
not the first calling.
Thank you
pro one, b
b[0] = 2
end
pro two
a = intarr(1)
a[*] = 1
one, a[*]
print, a
one, a
print, a
end
% Compiled module: ONE.
% Compiled module: TWO.
IDL> two
1
2
------------------------------------------------------------ ----------------
Daniel Sage
EPFL - Swiss Federal Institute of Technology - http://www.epfl.ch
BIG - Biomedical Imaging Group - http://bigwww.epfl.ch
Address: EPFL, DMT/IOA, BM, CH-1015 Lausanne, Switzerland
Tel: +41 21 693 5189
Fax: +41 21 693 3701
Email: daniel.sage@epfl.ch
------------------------------------------------------------ ----------------
|
|
|
Re: A strange behaviour of parameters passing [message #15685 is a reply to message #15630] |
Mon, 07 June 1999 00:00  |
David Kastrup
Messages: 33 Registered: February 1998
|
Member |
|
|
davidf@dfanning.com (David Fanning) writes:
> Rose (rmlongfield@my-deja.com) writes:
>
>> Um, David, don't you mean Gollum (Smeagul)?
>
> Uh, right. I *thought* that name wasn't right, but my
> kids weren't around and I couldn't put my hands on the
> book in the time I looked for it. :-(
>
> Cheers,
>
> David
>
> P.S. Let'ss jusssst, ssssay the S'ses isss what
> I rememberesses. :-)
Well, Bilbo tried riddle-talk on Smaug, too, but probably was less
successful there.
--
David Kastrup Phone: +49-234-700-5570
Email: dak@neuroinformatik.ruhr-uni-bochum.de Fax: +49-234-709-4209
Institut f�r Neuroinformatik, Universit�tsstr. 150, 44780 Bochum, Germany
|
|
|
|
|
Re: A strange behaviour of parameters passing [message #15694 is a reply to message #15630] |
Fri, 04 June 1999 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
William Daffer (vapuser@catspaw.jpl.nasa.gov) writes:
> There's nothing wrong with passing expressions, unless you want to
> change the value of the parameter inside a routine. It would be nice
> if they gave us some way of checking whether an expression, rather
> than a named variable, had been passed in. I am unaware of any such
> capability.
>
> I don't think you can call this is a bug; I think it's a feature.
While we are on the subject of passing by reference (IDL variables)
and passing by value (anything else, including expressions, system
variables, structure de-references, etc.), I should just point out
that this is *exactly* why this expression does NOT work in IDL:
FOR j=0, 10 DO Readf, lun, variable[j]
Although, I have to admit, it is an expression that every one
of us has used at least once (and many times if we come to IDL
from a FORTRAN background). People are still confused about why
it produces no error message (Answer: because it actual *works*),
and still returns no useful information.
In fact, here is a riddle Bilbo could have used with Smaug:
What works perfectly in IDL, but gives you no useful
information?
Answer: Reading into subscripted variables!
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: A strange behaviour of parameters passing [message #15696 is a reply to message #15630] |
Fri, 04 June 1999 00:00  |
Vapuser
Messages: 63 Registered: November 1998
|
Member |
|
|
Daniel SAGE <daniel.sage@epfl.ch> writes:
Yeah, the answer is don't pass expressions into routines if you want
the routine to change the value of its parameters.
In the first call you make to 'one' (e.g. one, a[*]) the parameter
is passed by _value_ since it's not a variable but an expression (I
think the completely proper way to say this is that what gets passed
is a reference to a temporary variable that is destroyed when the
routine 'one' exits) The second time you call 'one' the parameter is
passed by reference, since you are passing a named variable and not a
temporary one. In that case, you can change the contents of the
parameter and have that change persist after return.
There's nothing wrong with passing expressions, unless you want to
change the value of the parameter inside a routine. It would be nice
if they gave us some way of checking whether an expression, rather
than a named variable, had been passed in. I am unaware of any such
capability.
I don't think you can call this is a bug; I think it's a feature.
William
> Hi,
> Who can explain me the strange behaviour when I passed my parameters ?
> I create a small and stupid example to see the disfunctionning of IDL.
> Why the first call of the procedure 'one' returns 1 and the second call
> return 2 ? According the documentation of IDL, the second calling is
> correct but
> not the first calling.
> Thank you
>
> pro one, b
> b[0] = 2
> end
>
> pro two
> a = intarr(1)
> a[*] = 1
> one, a[*]
> print, a
> one, a
> print, a
> end
>
> % Compiled module: ONE.
> % Compiled module: TWO.
> IDL> two
> 1
> 2
>
> ------------------------------------------------------------ ----------------
> Daniel Sage
> EPFL - Swiss Federal Institute of Technology - http://www.epfl.ch
> BIG - Biomedical Imaging Group - http://bigwww.epfl.ch
>
> Address: EPFL, DMT/IOA, BM, CH-1015 Lausanne, Switzerland
> Tel: +41 21 693 5189
> Fax: +41 21 693 3701
> Email: daniel.sage@epfl.ch
> ------------------------------------------------------------ ----------------
--
William Daffer: 818-354-0161: vapuser@catspaw.jpl.nasa.gov
|
|
|
Re: A strange behaviour of parameters passing [message #15714 is a reply to message #15630] |
Fri, 04 June 1999 00:00  |
steinhh
Messages: 260 Registered: June 1994
|
Senior Member |
|
|
In article <3756C16A.A7C41C10@epfl.ch> Daniel SAGE
<daniel.sage@epfl.ch> writes:
> Hi,
> Who can explain me the strange behaviour when I passed my parameters ?
> I create a small and stupid example to see the disfunctionning of IDL.
> Why the first call of the procedure 'one' returns 1 and the second call
> return 2 ? According the documentation of IDL, the second calling is
> correct but
> not the first calling.
> Thank you
The behaviour is just as it should be, and just as documented.
See e.g. parameters:passing mechanism in the online help.
a[*] is an expression, hence a copy of a is made, and the
change in pro one affects only the copy.
Regards,
Stein Vidar
|
|
|
Re: A strange behaviour of parameters passing [message #15726 is a reply to message #15630] |
Thu, 03 June 1999 00:00  |
Liam Gumley
Messages: 473 Registered: November 1994
|
Senior Member |
|
|
Daniel SAGE wrote:
> Who can explain me the strange behaviour when I passed my parameters ?
> I create a small and stupid example to see the disfunctionning of IDL.
> Why the first call of the procedure 'one' returns 1 and the second call
> return 2 ? According the documentation of IDL, the second calling is
> correct but not the first calling.
[code removed]
I believe IDL is doing what it is supposed to do.
In the first call (one, a[*]), you are passing a subscripted array (even
though you requested all elements). Subscripted arrays are passed by
value and *cannot* be modified in the called procedure.
In the second call (one, a), you are passing an entire array. Entire
arrays are passed by reference and *can* be modified in the called
procedure.
Cheers,
Liam.
--
Liam E. Gumley
Space Science and Engineering Center, UW-Madison
http://cimss.ssec.wisc.edu/~gumley
|
|
|