1.7 About Variables and Assignments

Values may be assigned to variables. A variable enables you to refer to an object via a name. The name of a variable is called an identifier. The assignment operator is :=. There must be no white space between the : and the =. Do not confuse the assignment operator := with the single equality sign = which is in GAP only used for the test of equality.

    gap> a:= (9 - 7) * (5 + 6);
    22
    gap> a;
    22
    gap> a * (a + 1);
    506
    gap> a:= 10;
    10
    gap> a * (a + 1);
    110 

After an assignment the assigned value is echoed on the next line. The printing of the value of a statement may be in every case prevented by typing a double semicolon.

gap> w:= 2;;

After the assignment the variable evaluates to that value if evaluated. Thus it is possible to refer to that value by the name of the variable in any situation.

This is in fact the whole secret of an assignment. An identifier is bound to a value and from this moment points to that value. Nothing more. This binding is changed by the next assignment to that identifier. An identifier does not denote a block of memory as in some other programming languages. It simply points to a value, which has been given its place in memory by the GAP storage manager. This place may change during a GAP session, but that doesn't bother the identifier.

The identifier points to the value, not to a place in the memory.

For the same reason it is not the identifier that has a type but the object. This means on the other hand that the identifier a which now is bound to an integer value may in the same session point to any other value regardless of its type.

Identifiers may be sequences of letters and digits containing at least one letter. For example abc and a0bc1 are valid identifiers. But also 123a is a valid identifier as it cannot be confused with any number. Just 1234 indicates the number 1234 and cannot be at the same time the name of a variable.

Since GAP distinguishes upper and lower case, a1 and A1 are different identifiers. Keywords such as quit must not be used as identifiers. You will see more keywords in the following sections.

In the remaining part of this manual we will ignore the difference between variables, their names (identifiers), and the values they point at. It may be useful to think from time to time about what is really meant by terms such as the integer w.

There are some predefined variables coming with GAP. Many of them you will find in the remaining chapters of this manual, since functions are also referred to via identifiers.

This seems to be the right place to state the following rule.

The name of every function in the GAP library starts with a capital letter.

Thus if you choose only names starting with a small letter for your own variables you will not overwrite any predefined function.

But there are some further interesting variables one of which shall be introduced now.

Whenever GAP returns a value by printing it on the next line this value is assigned to the variable last. So if you computed

    gap> (9 - 7) * (5 + 6);
    22 

and forgot to assign the value to the variable a for further use, you can still do it by the following assignment.

    gap> a:= last;
    22 

Moreover there are variables last2 and last3, guess their values.

In this section you have seen how to assign values to variables. These values can later be accessed through the name of the variable, its identifier. You have also encountered the useful concept of the last variables storing the latest returned values. And you have learned that a double semicolon prevents the result of a statement from being printed.

Variables and assignments are described in more detail in Variables and Assignments. A complete list of keywords is contained in Keywords.

Previous Up Top Next
Index

GAP 3.4.4
April 1997