GEL is a dynamically scoped language. We will explain what this means below. That is, normal variables and functions are dynamically scoped. The exception are parameter variables, which are always global.
Like most programming languages, GEL has different types
of variables. Normally when a variable is defined in a function,
it is visible from that function and from all functions that are
called (all higher contexts). For example, suppose a function
f defines a variable
and then calls function g. Then
function g can reference
a. But once f returns,
a goes out of scope.
For example, the following code will print out 5.
The function g cannot be called on the
top level (outside f as
will not be defined).
function f() = (a:=5; g()); function g() = print(a); f();
If you define a variable inside a function it will override any variables defined in calling functions. For example, we modify the above code and write:
function f() = (a:=5; g()); function g() = print(a); a:=10; f();
ato 5 inside f does not change the value of
aat the top (global) level, so if you now check the value of
ait will still be 10.
Function arguments are exactly like variables defined inside the function, except that they are initialized with the value that was passed to the function. Other than this point, they are treated just like all other variables defined inside the function.
Functions are treated exactly like variables. Hence you can locally redefine functions. Normally (on the top level) you cannot redefine protected variables and functions. But locally you can do this. Consider the following session:
genius> function f(x) = sin(x)^2 = (`(x)=(sin(x)^2)) genius> function f(x) = sin(x)^2 = (`(x)=(sin(x)^2)) genius> function g(x) = ((function sin(x)=x^10);f(x)) = (`(x)=((sin:=(`(x)=(x^10)));f(x))) genius> g(10) = 1e20
Functions and variables defined at the top level are
considered global. They are visible from anywhere. As we
said the following function f
will not change the value of
a to 5.
a=6; function f() = (a:=5); f();
ato the value 3 you could call:
The set function always sets the toplevel global. There is no way to set a local variable in some function from a subroutine. If this is required, must use passing by reference.
So to recap in a more technical language: Genius operates with different numbered contexts. The top level is the context 0 (zero). Whenever a function is entered, the context is raised, and when the function returns the context is lowered. A function or a variable is always visible from all higher numbered contexts. When a variable was defined in a lower numbered context, then setting this variable has the effect of creating a new local variable in the current context number and this variable will now be visible from all higher numbered contexts.
There are also true local variables, which are not seen from anywhere but the current context. Also when returning functions by value it may reference variables not visible from higher context and this may be a problem. See the sections True Local Variables and Returning Functions.