GLOBAL Global Variables

Section: Variables and Arrays


Global variables are shared variables that can be seen and modified from any function or script that declares them. The syntax for the global statement is
  global variable_1 variable_2 ...

The global statement must occur before the variables appear.


Here is an example of two functions that use a global variable to communicate an array between them. The first function sets the global variable.

function set_global(x)
  global common_array
  common_array = x;

The second function retrieves the value from the global variable

function x = get_global
  global common_array
  x = common_array;

Here we exercise the two functions

--> set_global('Hello')
--> get_global

ans =