Re: Variable scoping for local variable in functions

From: <Oliver.Fuhrer_at_nyahnyahspammersnyahnyah>
Date: Fri Nov 13 2009 - 13:14:34 MST

Dave,

Great! I will wait for the next release. For the moment I am using a workaround with deletes at the end of the functions to clean away the variables within the function!

BTW: Should the return value of a function also be declared as "local" or not?

Cheers,
Oli

-----Original Message-----
From: David Brown [mailto:dbrown@ucar.edu]
Sent: Fri 11/13/2009 7:21 PM
To: Fuhrer Oliver
Cc: ncl-talk@ucar.edu
Subject: Re: Variable scoping for local variable in functions
 
Hi Oli,
I am a little surprised at the fact you are getting different results
for functions and procedures, because
I thought the problem existed for both. At any rate, this problem was
reported earlier this year by Melissa
Bukowsky and was fixed with a check-in (that applied both to functions
and procedures) on 9 September, 2009.
Running your code with the development version of NCL gives (as
complete output):
(0) this is test1
(0) this is test1
(0) this is test2
(0) this is test2

If you would like the new code let us know.
  -dave

On Nov 13, 2009, at 5:49 AM, <Oliver.Fuhrer@meteoswiss.ch> <Oliver.Fuhrer@meteoswiss.ch
> wrote:

> Hi ncl-talk,
>
> It seems that there is a strange quirk for local variables in
> functions
> which makes that they remain somehow globally defined in spite of
> being
> declared as local. Upon second entry the isvar() function returns true
> for such a variable but any reference to it give "variable undefined".
> This seems to be only the case for functions and not procedures.
> Here's
> an illustrative example which bugs and - in my humble opinion - should
> not.
>
> Any ideas?
> Oli
>
> ---------- script ------------
>
> undef("test1")
> procedure test1 ( )
> local dummy1
> begin
> print("this is test1")
> if (isvar("dummy1")) then
> print("deleting dummy1")
> delete(dummy1)
> end if
> dummy1 = 3
> end
>
> undef("test2")
> function test2 ( )
> local dummy2
> begin
> print("this is test2")
> if (isvar("dummy2")) then
> print("deleting dummy2")
> delete(dummy2)
> end if
> dummy2 = 3
> return(4)
> end
>
> ; first testing subroutine
> test1( )
> test1( )
>
> ; now testing function
> a = test2( )
> b = test2( )
>
> ---------- result ------------
>
>> ncl test6.ncl
> 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.
> (0) this is test1
> (0) this is test1
> (0) this is test2
> (0) this is test2
> (0) deleting dummy2
> fatal:Variable (dummy2) is undefined
> fatal:Execute: Error occurred at or near line 20 in file test6.ncl
>
> fatal:Execute: Error occurred at or near line 32 in file test6.ncl
>
>
>
> ________________________________________
>
> Oliver Fuhrer
> Numerical Models
>
> Federal Departement of Home Affairs FDHA
> Federal Office of Meteorology and Climatology MeteoSwiss
>
> Kraehbuehlstrasse 58, P.O. Box 514, CH-8044 Zurich, Switzerland
>
> Tel. +41 44 256 93 59
> Fax +41 44 256 92 78
> oliver.fuhrer@meteoswiss.ch
> www.meteoswiss.ch - First-hand information
>
> _______________________________________________
> 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 Nov 13 13:16:46 2009

This archive was generated by hypermail 2.1.8 : Mon Nov 23 2009 - 12:24:02 MST