## `:=` -- assign variables

#### Introduction

`x := value` assigns the variable `x` a `value`.

`[x1, x2...] := [value1, value2...]` assigns the variables `x1`, `x2` etc. the corresponding values `value1`, `value2` etc.

`f(X1, X2...) := value` adds an entry to the remember table of the procedure `f`.

#### Call(s)

x := value `_assign(x, value)`
[x1, x2...] := [value1, value2...] `_assign([x1, x2...], [value1, value2...])`
f(X1, X2, ...) := value `_assign(f(X1, X2, ...), value)`

#### Parameters

`x, x1, x2, ...` | - | identifiers or indexed identifiers
`value, value1, value2, ...` | - | arbitrary MuPAD objects
`f` | - | a procedure or a function environment
`X1, X2, ...` | - | arbitrary MuPAD objects

#### Returns

`value` or `[value1, value2...]`, respectively.

#### Related Functions

`anames`, `assign`, `assignElements`, `delete`, `evalassign`

#### Details

- `_assign(x, value)` is equivalent to `x := value`.
- `_assign([x1, x2...], [value1, value2...])` is equivalent to `[x1, x2...] := [value1, value2...]`. Both lists must have the same number of elements.

- If `x` is neither a list, nor a table, nor an array, nor a matrix, nor an element of a domain with a slot `"set_index"`, then an indexed assignment such as `x[i] := value` implicitly turns the identifier `x` into a table with a single entry `(i = value)`. Cf. example 2.

- The assignment `f(X1, X2...) := value` adds an entry to the remember table of the procedure `f`.

  If `f` is neither procedure nor a function environment, then `f` is implicitly turned into a (trivial) procedure with a single entry `(X1, X2, ...) = value` in its remember table. Cf. example 3.

- Identifiers on the left hand side of an assignment are not evaluated (use `evalassign` if this is not desired). I.e., in `x := value`, the previous value of `x`, if any, is deleted and replaced by the new value. Note, however, that the index of an indexed identifier is evaluated. I.e., in `x[i] := value`, the index `i` is replaced by its current value before the corresponding entry of `x` is assigned the value. Cf. example 4.
- `_assign` is a function of the system kernel.

#### Example 1

The assignment operator `:=` can be applied to a single identifier as well as to a list of identifiers:

    >> x := 42: [x1, x2, x3] := [43, 44, 45]: x, x1, x2, x3

                                  42, 43, 44, 45

In case of lists, all variables of the left-hand side are assigned their values simultaneously:

    >> [x1, x2] := [3, 4]: [x1, x2] := [x2, x1]: x1, x2

                                       4, 3

The functional equivalent of the assign operator `:=` is the function `_assign`:

    >> _assign(x, 13): _assign([x1, x2], [14, 15]): x, x1, x2

                                    13, 14, 15

Assigned values are deleted via the keyword `delete`:

    >> delete x, x1, x2: x, x1, x2

                                     x, x1, x2

#### Example 2

Assigning a value to an indexed identifier, a corresponding table (`table`, `DOM_TABLE`) is generated implicitly, if the identifier was not assigned a list, a table, an array, or a matrix before:

    >> delete x: x[1] := 7: x

                                      table(
                                        1 = 7
                                      )

If `x` is a list, a table, an array, or a matrix, then an indexed assignment adds a further entry or changes an existing entry:

    >> x[abc] := 8: x

                                    table(
                                      abc = 8,
                                      1 = 7
                                    )

    >> x := [a, b, c, d]: x[3] := new: x

                                  [a, b, new, d]

    >> x := array(1..2, 1..2): x[2, 1] := value: x

                              +-                  -+
                              |  ?[1, 1], ?[1, 2]  |
                              |                    |
                              |   value,  ?[2, 2]  |
                              +-                  -+

    >> delete x:

#### Example 3

Consider a simple procedure:

    >> f := x -> sin(x)/x: f(0)

          Error: Division by zero;
          during evaluation of 'f'

The following assignment adds an entry to the remember table:

    >> f(0) := 1: f(0)

                                         1

If `f` does not evaluate to a function, then a trivial procedure with a remember table is created implicitly:

    >> delete f: f(x) := x^2: expose(f)

                                proc()
                                  name f;
                                  option remember;
                                begin
                                  procname(args())
                                end_proc

Note that the remember table only provides a result for the input `x`:

    >> f(x), f(1.0*x), f(y)

                                 2
                                x , f(1.0 x), f(y)

    >> delete f:

#### Example 4

The left hand side of an assignment is not evaluated. In the following, `x := 3` assigns a new value to `x`, not to `y`:

    >> x := y: x := 3: x, y

                                       3, y

Consequently, the following is not a multiple assignment to the identifiers in the list, but a single assignment to the list `L`:

    >> L := [x1, x2]: L := [21, 22]: L, x1, x2

                                 [21, 22], x1, x2

However, indices are evaluated in indexed assignments:

    >> i := 2: x[i] := value: x

                                    table(
                                      2 = value
                                    )

    >> for i from 1 to 3 do x[i] := i^2 end_for: x

                                     table(
                                       3 = 9,
                                       1 = 1,
                                       2 = 4
                                     )

    >> delete x, L, i:

#### Example 5

Since an assignment has a return value (the assigned value), the following command assigns values to several identifiers simultaneously:

    >> a := b := c := 42: a, b, c

                                    42, 42, 42

For syntactical reasons, the inner assignment has to be enclosed by additional brackets in the following command:

    >> a := sin((b := 3)): a, b

                                     sin(3), 3

    >> delete a, b, c:

#### Changes

- In previous releases, `NIL` was used to unassign an identifier. Now, `NIL` is handled as any other expression and the command `delete` must be used to delete values.

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000