Re: scope of variable in NCL

From: Wei Huang <huangwei_at_nyahnyahspammersnyahnyah>
Date: Fri Oct 16 2009 - 09:38:28 MDT

Daryl,

They have "a = 1"
before function definition.

Wei Huang
huangwei@ucar.edu
VETS/CISL
National Center for Atmospheric Research
P.O. Box 3000 (1850 Table Mesa Dr.)
Boulder, CO 80307-3000 USA
(303) 497-8924

On Oct 16, 2009, at 9:34 AM, daryl herzmann wrote:

> Hi,
>
> For what it is worth, I am puzzled by how the original example even
> worked.
>
> Copyright (C) 1995-2009 - All Rights Reserved
> University Corporation for Atmospheric Research
> NCAR Command Language Version 5.1.1
> The use of this software is governed by a License Agreement.
> See http://www.ncl.ucar.edu/ for more details.
> ncl 0> function test(a,b,c)
> ncl 1> local a,b,c
> ncl 2> begin
> ncl 3> a=-999
> ncl 4> return(1)
> ncl 5> end
> ncl 6> print(""+test(a,1,2))
> fatal:Variable (a) is undefined
> fatal:Execute: Error occurred at or near line 6
>
> daryl
>
> On Fri, 16 Oct 2009, David Brown wrote:
>
>> Hi Saji,
>> Wei is quite correct about this case. Arguments are passed by
>> reference and the variables they reference can be modified inside the
>> function. The 'local' statement has no meaning for arguments. You can
>> ignore the last message I sent. I apologize that I did not notice
>> that
>> 'a' was being used as an argument to the function.
>> -dave
>>
>> On Oct 16, 2009, at 8:09 AM, Wei Huang wrote:
>>
>>> Saji,
>>>
>>> That is because you use "a" as an argument element, and local.
>>>
>>> If you do "print("" + test(0,1,2))", then you can not change "a"
>>> out side your function.
>>>
>>> Wei Huang
>>> huangwei@ucar.edu
>>> VETS/CISL
>>> National Center for Atmospheric Research
>>> P.O. Box 3000 (1850 Table Mesa Dr.)
>>> Boulder, CO 80307-3000 USA
>>> (303) 497-8924
>>>
>>>
>>>
>>>
>>>
>>> On Oct 15, 2009, at 11:03 PM, Saji N Hameed wrote:
>>>
>>>> Even declaring "a" as local does not seem to work :(
>>>> ncl 1> function test(a,b,c)
>>>> ncl 2> local a,b,c
>>>> ncl 3> begin
>>>> ncl 4> a=-999
>>>> ncl 5> return(1)
>>>> ncl 6> end
>>>> ncl 7> print(""+test(a,1,2))
>>>> (0) 1
>>>> ncl 8> print(""+a)
>>>> (0) -999
>>>>
>>>> * Saji N Hameed <saji@apcc21.net> [2009-10-16 13:59:15 +0900]:
>>>>
>>>>> Dear NCL Developers,
>>>>>
>>>>> I suppose that the following should not happen. Isn't it a bug?
>>>>> We are told that variables inside a function have local scope, and
>>>>> this unusual feature can lead to a lot of headaches. Any
>>>>> solutions,
>>>>> other than "local a" or using another variable name than "a"
>>>>>
>>>>> Copyright (C) 1995-2009 - All Rights Reserved
>>>>> University Corporation for Atmospheric Research
>>>>> NCAR Command Language Version 5.1.1
>>>>> The use of this software is governed by a License Agreement.
>>>>> See http://www.ncl.ucar.edu/ for more details.
>>>>> ncl 0> a=5
>>>>> ncl 1> function testme(a,b,c)
>>>>> ncl 2> begin
>>>>> ncl 3> a=2
>>>>> ncl 4> return(1)
>>>>> ncl 5> end
>>>>> ncl 6> print(""+a)
>>>>> (0) 5
>>>>> ncl 7> print(""+testme(a,1,2))
>>>>> (0) 1
>>>>> ncl 8> print(""+a)
>>>>> (0) 2
>>>>>
>>>>> Thanks,
>>>>> saji
>>>>> --
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> ncl-talk mailing list
>>>>> List instructions, subscriber options, unsubscribe:
>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>>
>>>>
>>>> --
>>>> Saji N. Hameed
>>>>
>>>> APEC Climate Center
>>>> 1463 U-dong, Haeundae-gu, +82 51 745
>>>> 3951
>>>> BUSAN 612-020, KOREA saji@apcc21.net
>>>> Fax: +82-51-745-3999
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> ncl-talk mailing list
>>>> List instructions, subscriber options, unsubscribe:
>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Oct 16 09:38:32 2009

This archive was generated by hypermail 2.1.8 : Thu Oct 22 2009 - 13:09:30 MDT