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 a
and then calls function g
. Then
function g
can reference
a
. But once f
returns,
the variable 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 a
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();
This code will still print out 5. But if you call
g
outside of f
then
you will get a printout of 10. Note that
setting a
to 5 inside f
does not change
the value of a
at the top (global) level,
so if you now check the value of a
it will
still be 10.
Les arguments de fonction sont exactement comme les variables définies à l'intérieur de la fonction sauf qu'ils sont initialisés à la valeur qui a été transmise à la fonction. En dehors de ce point, ils sont traités exactement comme toutes les autres variables définies dans la fonction.
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();
Sometimes, however, it is necessary to set
a global variable from inside a function. When this behavior is needed,
use the
set
function. Passing a string or a quoted identifier to
this function sets the variable globally (on the top level).
For example, to set
a
to the value 3 you could call:
set(`a,3)
or:
set("a",3)
La fonction set
définit toujours au niveau global supérieur. Il n'est pas possible de définir une variable locale dans des fonctions à partir d'un sous-programme. Si c'est nécessaire, vous devez utiliser la transmission par référence.
See also the
SetElement
and
SetVElement
functions.
Donc pour récapituler dans un langage plus technique : genius opère avec différents contextes numérotés. Le niveau supérieur est le contexte 0 (zéro). À chaque fois qu'une fonction est entrée, le contexte est augmenté et lorsqu'une fonction est quittée, le contexte est diminué. Une fonction ou une variable est toujours visible à partir de tous les contextes de numéro plus élevé. Si une variable a été définie dans un contexte de numéro plus bas alors attribuer une valeur à cette variable a pour effet de créer une nouvelle variable locale dans le numéro de contexte actuel et cette variable est maintenant visible de tous les contextes de numéro plus élevé.
There are also true local variables that 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.