Variable scoping for local variable in functions

From: <Oliver.Fuhrer_at_nyahnyahspammersnyahnyah>
Date: Fri Nov 13 2009 - 05:49:26 MST

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
Received on Fri Nov 13 05:49:34 2009

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