# 5.3 Assignment

> `x <- value` stores a new value in a variable and forgets the one it had.

Web page: https://pseudolearn.app/en/spec/declaraciones/asignacion

> **In one sentence**
>
> `x <- value` stores a new value in a variable and forgets the one it had.

## 5.3.1 How to write it

```pseudolearn
«destino» <- «expresión»;
```

> **Parts**
>
> - «target» is a [designator](https://pseudolearn.app/en/spec/declaraciones/designadores.md): a variable, an array cell or an attribute.
> - `<-` is the assignment arrow. It reads "receives".
> - «expression» computes the new value. Its type must fit that of the target.
> - `;` ends the statement.

## 5.3.2 How it works

Assignment happens in three stages, always in this order:

1. The target is evaluated: the variable and, if any, its indices.
2. The right-hand expression is evaluated, with the values that exist **before** assigning.
3. The result is stored in the target.

That is why `contador <- contador + 1` works: the right side reads the old value and the result replaces the old one.

The classic example is swapping two values. A third variable is needed, because assigning loses what was there:

### Program: Swapping two variables

```pseudolearn
Algorithm Intercambio
  Define a As Integer <- 1;
  Define b As Integer <- 2;
  Define auxiliar As Integer;
  auxiliar <- a;
  a <- b;
  b <- auxiliar;
  Write a, " ", b;
EndAlgorithm
```

**Output**

```text
2 1
```

*The program finishes normally.*

**Figure 5.3-1: Swapping with an auxiliary**

Three boxes a, b and auxiliar. a is 1 and b is 2. First auxiliar receives 1, then a receives 2 and finally b receives 1 from auxiliar.

Swapping two values needs an auxiliary box that keeps the first one before it is overwritten.

1. Step 1: Starting point: `a` is 1 and `b` is 2.
2. Step 2: `auxiliar <- a;` copies the 1 into the auxiliary box.
3. Step 3: `a <- b;` overwrites the 1 in `a` with a 2. The 1 is still safe in `auxiliar`.
4. Step 4: `b <- auxiliar;` gets the 1 back. The values are swapped.

Assigning an object copies the **reference**, not the object: afterwards both variables name the same object. A whole array is not assigned in one go.

> **Common mistakes**
>
> The type on the right must fit the target:
>
> ### Program: A text in an integer variable
>
> ```pseudolearn
> Algorithm Tipos
>   Define x As Integer <- 1;
>   x <- "uno";
>   Write x;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `incompatibleTypesInAssignment` (error, 3:3): Incompatible types in assignment: cannot assign string to integer
>
> *The program finishes with an error.*
>
> An index out of range is only discovered when running:
>
> ### Program: Assigning outside the array
>
> ```pseudolearn
> Algorithm Fuera
>   Dimension notas[3] As Integer;
>   notas[3] <- 1;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `arrayIndexOutOfRange` (error, 3:3): Index 3 is out of range for an array of size 3
>
> *The program finishes with an error.*

> **Under the hood**
>
> Every assignment to a local variable is a `store` in its slot. Notice that `auxiliar <- a` is a `load` from the slot of `a` followed by a `store` in that of `auxiliar`.
>
> **Intermediate representation**
>
> ```text
> module Intercambio en
> func suspendible @main() -> unit {
> b0() mass 7:
>   stmt.enter @2:3-2:27
>   %0 = const.i64 1 @2:26-2:27
>   stmt.exit @2:3-2:27
>   stmt.enter @3:3-3:27
>   %1 = const.i64 2 @3:26-3:27
>   stmt.exit @3:3-3:27
>   stmt.enter @4:3-4:29
>   %2 = value.undefined i64 @4:3-4:29
>   stmt.exit @4:3-4:29
>   stmt.enter @5:3-5:16
>   %3 = value.reinterpret i64, %0 @5:3-5:16
>   stmt.exit @5:3-5:16
>   stmt.enter @6:3-6:9
>   %4 = value.reinterpret i64, %1 @6:3-6:9
>   stmt.exit @6:3-6:9
>   stmt.enter @7:3-7:16
>   %5 = value.reinterpret i64, %3 @7:3-7:16
>   stmt.exit @7:3-7:16
>   stmt.enter @8:3-8:18
>   %6 = const.char ' ' @8:12-8:15
>   out.write %4, %6, %5, newline @8:3-8:18
>   stmt.exit @8:3-8:18
>   return @9:1-9:13
> }
> ```

> **Note**
>
> It is easy to read `a <- b` as a mathematical equality. Draw the boxes and explain why `a <- b; b <- a;` swaps nothing.

**In short**
- `<-` stores a new value and forgets the previous one.
- First the target, then the right side, finally the store.
- The type must fit; indices are checked when running.

**See also**
- [5.4 Designators](https://pseudolearn.app/en/spec/declaraciones/designadores.md)
- [5.1 Declaring variables with Define](https://pseudolearn.app/en/spec/declaraciones/definir.md)
- [9.6 Copies, references and identity](https://pseudolearn.app/en/spec/objetos/copia.md)
