Contents Previous Next Subchapters

Using Variables And Constants With Functions
Syntax global variable list
See Also const , and local variables

Description
Enables a function to access previously defined variables.

Tutorial
Variables that have global scope are not accessible within a function unless they are specified in a global statement. If you enter
     clear
     x = 5
     function f() begin
          x = 3
          print x
     end
     f
O-Matrix will respond
     3
The statement that assigned 3 to x created a variable x that has function scope ; i.e., its scope is limited to the function f. Outside of the function, x has the value 5. Thus, if you continue the example above by entering
     print x
O-Matrix will respond
     5
You can use the global statement to access and modify variables that have global scope. If you enter
     clear
     x = 5
     function f() begin
          global x
          x = 3
     end
     f
     print x
O-Matrix will respond
     3


Constants defined outside of any function do not need a global statement to be accessed within a function. If you enter
     clear
     const x = 5
     function f() begin
          print x
     end
     f
O-Matrix will respond
     5


If a global statement is not within a function, it will make the corresponding variables accessible in all the subsequent functions within the current file. If the file TEMP.OMS contains
     global X, Y
     function f() begin
          print X
     end
     function g() begin
          print Y
     end
and at the command line you enter
     clear
     X = 1
     Y = 2
     include TEMP.OMS
     f
O-Matrix will respond
     1
If you then enter
     g
O-Matrix will respond
     2

Reference
If a global statement is not within a function, it must be within a file (not at the command line).