- publishing free software manuals
GNU Octave Manual
by John W. Eaton
Paperback (6"x9"), 324 pages, 4 figures
ISBN 0954161726
RRP £19.99 ($29.99)

Get a printed copy>>>

7.1 Global Variables

A variable that has been declared global may be accessed from within a function body without having to pass it as a formal parameter.

A variable may be declared global using a global declaration statement. The following statements are all global declarations.

global a
global b = 2
global c = 3, d, e = 5

It is necessary declare a variable as global within a function body in order to access it. For example,

global x
function f ()
  x = 1;
endfunction
f ()

does not set the value of the global variable x to 1. In order to change the value of the global variable x, you must also declare it to be global within the function body, like this

function f ()
  global x;
  x = 1;
endfunction

Passing a global variable in a function parameter list will make a local copy and not modify the global value. For example, given the function

function f (x)
  x = 0
endfunction

and the definition of x as a global variable at the top level,

global x = 13

the expression

f (x)

will display the value of x from inside the function as 0, but the value of x at the top level remains unchanged, because the function works with a copy of its argument.

Built-in Variable: warn_comma_in_global_decl
If the value of warn_comma_in_global_decl is nonzero, a warning is issued for statements like

global a = 1, b

which makes the variables a and b global and assigns the value 1 to the variable a, because in this context, the comma is not interpreted as a statement separator.

The default value of warn_comma_in_global_decl is nonzero.

Built-in Variable: initialize_global_variables
If the value of initialize_global_variables is nonzero, global variables are initialized to the value of the built-in variable default_global_variable_value.

the default value of initialize_global_variables is zero.

Built-in Variable: default_global_variable_value
If initialize_global_variables is nonzero, the value of default_glbaol_variable_value is used as the initial value of global variables that are not explicitly initialized. for example,

initialize_global_variables = 1;
default_global_variable_value = 13;
global foo;
foo
     => 13

the variable default_global_variable_value is initially undefined.

Built-in Function: is_global (name)
Return 1 if name is globally visible. Otherwise, return 0. For example,

global x
is_global ("x")
     => 1
ISBN 0954161726GNU Octave ManualSee the print edition