Global variables are undefined in functions.
Ben Abbott
bpabbott at mac.com
Tue May 12 10:13:49 CDT 2009
On Tuesday, May 12, 2009, at 03:41AM, "Sparbier, Jochen, Dr." <jochen.sparbier at hach-lange.de> wrote:
>To: bug at octave.org
>Cc: jochen.sparbier at hach-lange.de
>Subject: Global variables are undefined in functions.
>--------
>Bug report for Octave 3.0.5 configured for i686-pc-linux-gnu
>
>Description:
>-----------
>A simple test script (test_globals.m) defines a global variable (g_factor),
>which is used in a simple user defined function (help_func.m).
>Octave complains about an undefined symbol:
>"error: `g_factor' undefined near line 4 column 10"
>
>
>% file : test_globals.m
>% Test of global variable usage
>clear all
>global g_factor = 2
>g_factor
>A = help_func(1)
>% endoffile: test_globals.m
>
>
>% file : help_func.m
>% Test function, which uses a global variable
>function h = help_func(M)
> h = M * g_factor;
>endfunction
>%endoffile : help_func.m
>
If you want to use a global variable in a function, you'll need to declare it.
% Test function, which uses a global variable
function h = help_func(M)
global g_factor
h = M * g_factor;
endfunction
Ben
More information about the Bug-octave
mailing list