NCL variable scope

From: <Oliver.Fuhrer_at_nyahnyahspammersnyahnyah>
Date: Fri May 13 2011 - 06:39:46 MDT

Dear NCL team,

I have a question about variable scope and good programming practice in
NCL. The following code...

;=== BEGIN ===
undef("gugus")
procedure gugus( x:numeric )
begin
 a = x
end

a = 1

print(a)
gugus(2)
print(a)
;=== END ===

...outputs two times "1". So the scope of a in the procedure gugus seems
to be local. Moving the declaration of a before the declaration of
gugus...

;=== BEGIN ===
a = 1

undef("gugus")
procedure gugus( x:numeric )
begin
 a = x
end

print(a)
gugus(2)
print(a)
;=== END ===

...outputs "1" and then "2", so the scope of a in the procedure gugus
now seems to be global, since the symbol is known when the procedure
definition is encountered. Adding a "local a" statement to the procedure
definition will again give two times "1" in the output. My questions are
the following...

- Is that the intended behaviour of NCL? Forgetting to declare variable
as local can have quite dramatic consequences which depend on what code
has been executed before the procedure call (i.e. has a already been
used and defined or not).

- Say I want to write a library "utilities.ncl" of NCL scripts which use
a global variable "gDebug = True/False" to control verbose debugging
output of the library procedures. It is probably good NCL programming
practice to put all of these global variables at the beginning of
"utilities.ncl" in such a way...

undef("gDebug")
gDebug = False

...in order to define the globals and inititalize them with default
values. A NCL script can then load "utilities.ncl" and redefined gDebug
= True as needed. Is there any standard practice to check wether the
symbol gDebug is already in use and issue a warning upon the load
"utilities.ncl" that there is a name conflict?

- I am assuming that all procedure/function arguments in NCL are
automatically "local" by definition, correct? For example, the program
below correctly gives two times "4" for x.

;=== BEGIN ===
a = 1
x = 4

undef("gugus")
procedure gugus( x:numeric )
begin
 a = x
 x = 3
end

print((/a,x/))
gugus(2)
print((/a,x/))
;=== END ===

Thanks and kind regards,
Oli
 
________________________________________

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
Received on Fri May 13 06:39:59 2011

This archive was generated by hypermail 2.1.8 : Wed May 25 2011 - 09:35:33 MDT