next | previous | forward | backward | up | top | index | toc | Macaulay2 website
Macaulay2Doc > The Macaulay2 language > variables > assigning values

assigning values

Use an equal sign to assign values to variables.
i1 : x = "abcde"

o1 = abcde
i2 : x

o2 = abcde
Before assignment, any reference to a variable provides the symbol with that name. After assignment, the assigned value is provided. The variable created is global, in the sense that any code placed elsewhere that contains a reference to a variable called x will refer to the one we just set.

It is important to distinguish between a symbol and its value. The initial value of a global symbol is the symbol itself, and the initial value of a local variable is null. One possibility for confusion comes from the possibility of having a symbol whose value is another symbol; it's even more confusing if the two symbols have the same name but different scopes, for example, if one of them is global and the other is local.

i3 : y

o3 = y

o3 : Symbol
i4 : y = z

o4 = z

o4 : Symbol
i5 : y

o5 = z

o5 : Symbol
i6 : z = 444

o6 = 444
i7 : z

o7 = 444
i8 : y

o8 = z

o8 : Symbol
In the example above, the final value of y is the symbol z, even though the symbol z has acquired a value of its own. The function value can be used to get the value of a symbol.
i9 : value y

o9 = 444
The operator <- can be used to set the value of a symbol. This operator differs from = in that the symbol or expression on the left hand side is evaluated.
i10 : y <- 555

o10 = 555
i11 : y

o11 = z

o11 : Symbol
i12 : z

o12 = 555
i13 : y = 666

o13 = 666
i14 : y

o14 = 666
i15 : z

o15 = 555
One reason the user needs to understand this concept is that assignments with the operator <- are occasionally done on the user's behalf by bits of code already in the system, for example, when creating a polynomial ring the prospective variables are given new values that are the generators of the polynomial ring.

See also