Skip to main content
Logo image

Section 8.4 Global Variables

It is possible to declare variables that have global scope; that is, every function can see and modify their value. This is accomplished using the global command:
global variable_name(s)
For example:
global ThermalCond EarthRad NStudents
The variable must be declared global in every file that uses it. It needs to be declared in the Command Window and Workspace if it is to be used there.
The global declaration must appear before the variable is used.
The variable can then be assigned or reassigned any value in any of the locations it is declared.
Use global variables with caution. It’s easy to forget what other parts of your code do to a variable. If you do choose to use global variables then it is recommended to declare these at the top of each file that uses them.