[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Puzzled about scope/existence of globals
From: |
John W. Eaton |
Subject: |
Puzzled about scope/existence of globals |
Date: |
Sat, 22 Nov 2003 21:08:29 -0600 |
On 22-Nov-2003, Glenn Golden <address@hidden> wrote:
| Given the function foo(), defined as
|
| function foo()
| global g;
| exist("g", "var")
| endfunction
|
| I found the following to be surprising:
|
| 1> foo
| ans = 0 # ok
This can happen in versions of Octave before 2.1.51 because global
variables are not initialized by default. You can change this
behavior with the built-in variables default_global_variable_value and
initialize_global_variables. In 2.1.51 and later, these variables are
gone and for compatibility with Matlab, Octave always initializes
global variables to [] (unless you provide your own initializer in the
global statement).
| 2> global g
|
| 3> g.barf = 1;
|
| 4> foo
| ans = 1 # ok
|
| 5> clear g
|
| 6> whos g
| # ok
| 7> foo
| ans = 1 # huh?
You cleared the link to the global g in the top-level workspace, but
not in the function foo, which still exists. Clear foo, and g should
really go away (unless there is some other function that still has it
as a global).
jwe
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------
Re: Puzzled about scope/existence of globals, Tomer Altman, 2003/11/22