| GNU Octave Manual by John W. Eaton Paperback (6"x9"), 324 pages, 4 figures ISBN 0954161726 RRP £19.99 ($29.99) |
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_declis nonzero, a warning is issued for statements likeglobal a = 1, b
which makes the variables
aandbglobal and assigns the value 1 to the variablea, because in this context, the comma is not interpreted as a statement separator.The default value of
warn_comma_in_global_declis nonzero.
- Built-in Variable: initialize_global_variables
- If the value of
initialize_global_variablesis nonzero, global variables are initialized to the value of the built-in variabledefault_global_variable_value.the default value of
initialize_global_variablesis zero.
- Built-in Variable: default_global_variable_value
- If
initialize_global_variablesis nonzero, the value ofdefault_glbaol_variable_valueis 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 => 13the variable
default_global_variable_valueis 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 0954161726 | GNU Octave Manual | See the print edition |