FreeMat

- Main Page
- Related Pages

GLOBAL Global Variables

Section: Variables and Arrays

# Usage

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.

# Example

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.

         set_global.m

    function set_global(x)
      global common_array
      common_array = x;

The second function retrieves the value from the global variable

         get_global.m

    function x = get_global
      global common_array
      x = common_array;

Here we exercise the two functions

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

    ans =
    Hello