Scoping rules define the contexts where variables are available. HyperMath has three scopes where variables can be defined:
Option |
Defiinition |
Base Scope |
Variables defined outside functions fall in this scope. They are accessible outside functions only. |
Function Scope |
Variables defined inside functions fall in this scope. They are accessible inside the defined functions only. Each function has its own scope. That is the case for inner functions, also. |
Global Scope |
Variables explicitly declared as global, whether outside or inside functions, fall in this scope. These global variables can be accessed from any scope as long as they are also explicitly declared as global in there. |
For example, the following code defines two variables of same name in different scopes:
a = 0; // defined in Base
function foo()
a = 10; // defined in function scope
print(a)
end
foo();
print(a); // prints the variable defined in Base scope
The output would be:
10
0
Variables can be declared global as follows (note that values cannot be assigned to them at the same time):
global var1, …, varN; // the semi-colon is required
The above example can be modified to make the variable global such that it is accessible inside the function, too:
global a;
a = 0;
function foo()
global a; // have to define as global here also
a = 10; // this modifies the global variable instead of defining
// a new one in the function
print(a)
end
foo();
print(a);
The output now would be:
10
10