Re: brain twister [message #22797] |
Fri, 08 December 2000 15:02 |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
Todd Clements wrote:
>
> Paul van Delst <pvandelst@ncep.noaa.gov> wrote:
>
>>> Okay, enough ideology for one week.
>>
>> That's *almost* a pun. :o)
>
> Wow, I missed that one. Getting slow, I guess.
>
>>> ----- Example 2
>>> .comp
>>> function test2, x
>>> return, x
>>> end
>>>
>>> a = dindgen(5)
>>> (test2(a))[2] = -2
>>> print, a
>>
>> You're right...this is weird. If you do a help:
>>
>> IDL> help, test2(a)
>> A DOUBLE = Array[5]
>>
>> How come the result isn't
>>
>> <Expression> DOUBLE = Array[5]
>
> I wouldn't expect the result to be <Expression> becaues the result _is_ A,
> so I would expect IDL to tell me that the result is A. We pass in a by
> reference, return it, and so we should expect to get it back. I'm happy
> that IDL can keep track of the variable through all that mucking around...
> it makes sense to me.
Fair enough. I'm thinking in Fortran mode where there is no standard requirement for parameter
passing to be by value or by reference. I forgot IDL isn't like that.
After reading the online docs, you're absolutely correct that the result is "A" and not an
expression. The result does make sense. After much thought and reflection at least. :o)
paulv
--
Paul van Delst Ph: (301) 763-8000 x7274
CIMSS @ NOAA/NCEP Fax: (301) 763-8545
Rm.207, 5200 Auth Rd. Email: pvandelst@ncep.noaa.gov
Camp Springs MD 20746
|
|
|
Re: brain twister [message #22800 is a reply to message #22797] |
Fri, 08 December 2000 13:10  |
mole6e23
Messages: 31 Registered: December 1998
|
Member |
|
|
Paul van Delst <pvandelst@ncep.noaa.gov> wrote:
>> Okay, enough ideology for one week.
>
> That's *almost* a pun. :o)
Wow, I missed that one. Getting slow, I guess.
>> ----- Example 2
>> .comp
>> function test2, x
>> return, x
>> end
>>
>> a = dindgen(5)
>> (test2(a))[2] = -2
>> print, a
>
> You're right...this is weird. If you do a help:
>
> IDL> help, test2(a)
> A DOUBLE = Array[5]
>
> How come the result isn't
>
> <Expression> DOUBLE = Array[5]
I wouldn't expect the result to be <Expression> becaues the result _is_ A,
so I would expect IDL to tell me that the result is A. We pass in a by
reference, return it, and so we should expect to get it back. I'm happy
that IDL can keep track of the variable through all that mucking around...
it makes sense to me.
Todd
|
|
|
Re: brain twister [message #22802 is a reply to message #22800] |
Fri, 08 December 2000 11:37  |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
Craig Markwardt wrote:
>
> Okay, enough ideology for one week.
That's *almost* a pun. :o)
>
> ----- Example 2
> .comp
> function test2, x
> return, x
> end
>
> a = dindgen(5)
> (test2(a))[2] = -2
> print, a
You're right...this is weird. If you do a help:
IDL> help, test2(a)
A DOUBLE = Array[5]
How come the result isn't
<Expression> DOUBLE = Array[5]
???
--
Paul van Delst Ph: (301) 763-8000 x7274
CIMSS @ NOAA/NCEP Fax: (301) 763-8545
Rm.207, 5200 Auth Rd. Email: pvandelst@ncep.noaa.gov
Camp Springs MD 20746
|
|
|
Re: brain twister [message #22806 is a reply to message #22802] |
Fri, 08 December 2000 09:59  |
Pavel A. Romashkin
Messages: 531 Registered: November 2000
|
Senior Member |
|
|
Well, try this. I think it is doing exactly what you expect given that A
is passed by reference, no matter how bizarre it looks.
.comp
pro test1, x, y
x[2] = 10.
print, y
end
a = findgen(10)
print, a
test1, a, a
X and Y are "pointers" to the same array A.
Cheers,
Pavel
|
|
|
Re: brain twister [message #22808 is a reply to message #22806] |
Fri, 08 December 2000 08:58  |
mole6e23
Messages: 31 Registered: December 1998
|
Member |
|
|
craigmnet@cow.physics.wisc.edu wrote:
> Okay, enough ideology for one week. Here are some brain twisters for
> you. See if you can decide what the output should be without running
> it. In each example I give the commands to paste into the command
> line, ending with some main-level commands to enter.
I was able to guess number two, but I never would have guessed at number
one. I'm assuming this is some sort of memory saving device? I notice that
if you pass a,a+1 you get something different, which, after having seen
the results of example 1, you might be able to expect.
Look at the two following examples for another interesting behavior
(although not quite as interesting by any means!):
;; begin
.comp
pro test1, x, y, z
help, y, x, z
end
.comp
pro test2, y, x, z
help, z, y, z
end
a=dindgen(5)
test1,a,a,a
test2,a,a,a
;; end
That surprised me a little at first, but I guess it makes sense. You have
to do something with your variables in the program.
I think example two makes a lot of sense...you are passing the parameter
directly. If you can modify x within the function/procedure, why not
actually return x?
Neat examples, though!
Todd
>
> ----- Example 1
> .comp
> pro test1, x, y
> help, x, y
> end
>
> a = dindgen(5)
> test1, a, a
>
> ----- Example 2
> .comp
> function test2, x
> return, x
> end
>
> a = dindgen(5)
> (test2(a))[2] = -2
> print, a
>
> These were a bit of a surprise for me!
> Craig
|
|
|