2.7 Variables

A variable is a location in a GAP program that points to a value. We say the variable is bound to this value. If a variable is evaluated it evaluates to this value.

Initially an ordinary variable is not bound to any value. The variable can be bound to a value by assigning this value to the variable (see Assignments). Because of this we sometimes say that a variable that is not bound to any value has no assigned value. Assignment is in fact the only way by which a variable, which is not an argument of a function, can be bound to a value. After a variable has been bound to a value an assignment can also be used to bind the variable to another value.

A special class of variables are arguments of functions. They behave similarly to other variables, except they are bound to the value of the actual arguments upon a function call (see Function Calls).

Each variable has a name that is also called its identifier. This is because in a given scope an identifier identifies a unique variable (see Identifiers). A scope is a lexical part of a program text. There is the global scope that encloses the entire program text, and there are local scopes that range from the function keyword, denoting the beginning of a function definition, to the corresponding end keyword. A local scope introduces new variables, whose identifiers are given in the formal argument list and the local declaration of the function (see Functions). Usage of an identifier in a program text refers to the variable in the innermost scope that has this identifier as its name. Because this mapping from identifiers to variables is done when the program is read, not when it is executed, GAP is said to have lexical scoping. The following example shows how one identifier refers to different variables at different points in the program text.

     g := 0;            # global variable g
     x := function ( a, b, c )
        local   y;
        g := c;         # c refers to argument c of function x
        y := function ( y )
            local  d, e, f;
            d := y;     # y refers to argument y of function y
            e := b;     # b refers to argument b of function x
            f := g;     # g refers to global variable g
            return d + e + f;
        end;
        return y( a );  # y refers to local y of function x
    end; 

It is important to note that the concept of a variable in GAP is quite different from the concept of a variable in programming languages like PASCAL. In those languages a variable denotes a block of memory. The value of the variable is stored in this block. So in those languages two variables can have the same value, but they can never have identical values, because they denote different blocks of memory. (Note that PASCAL has the concept of a reference argument. It seems as if such an argument and the variable used in the actual function call have the same value, since changing the argument's value also changes the value of the variable used in the actual function call. But this is not so; the reference argument is actually a pointer to the variable used in the actual function call, and it is the compiler that inserts enough magic to make the pointer invisible.) In order for this to work the compiler needs enough information to compute the amount of memory needed for each variable in a program, which is readily available in the declarations PASCAL requires for every variable.

In GAP on the other hand each variable justs points to a value.

Previous Up Top Next
Index

GAP 3.4.4
April 1997