# 5.1 Declaring variables with Define

> `Define` creates variables: named, typed boxes where the program keeps a value that can change.

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

> **In one sentence**
>
> `Define` creates variables: named, typed boxes where the program keeps a value that can change.

## 5.1.1 How to write it

```pseudolearn
Define «nombre» As «tipo» ⟦<- «valor inicial»⟧;
Define «nombre», «nombre» As «tipo»;
```

> **Parts**
>
> - `Define` opens the declaration.
> - «name» is an identifier. A single letter is valid too.
> - `As` separates the names from the type.
> - «type» says what kind of values it can hold: `Integer`, `Real`, `String`…
> - `<-` and the «initial value» are optional, and only allowed with a single name.
> - `;` ends the declaration.

## 5.1.2 How it works

A **variable** is a name bound to a place in memory. `Define` creates that place with the given type. With an initial value, the value is stored at once; without one, the box stays empty until the first assignment.

### Program: Variables in a backpack

```pseudolearn
Algorithm Mochila
  Define libros, cuadernos As Integer;
  Define nombre As String <- "Leo";
  libros <- 3;
  cuadernos <- libros * 2;
  Write nombre, " lleva ", libros, " libros y ", cuadernos, " cuadernos";
EndAlgorithm
```

**Output**

```text
Leo lleva 3 libros y 6 cuadernos
```

*The program finishes normally.*

**Figure 5.1-1: Variable boxes**

Three boxes in memory: libros and cuadernos, of type Integer, start empty; nombre, of type String, starts with "Leo". Then libros receives 3 and cuadernos receives 6, computed by reading libros.

Every `Define` creates a box with a name and a type. Those without an initial value start empty.

1. Step 1: `Define libros, cuadernos As Integer;` creates two empty boxes.
2. Step 2: `Define nombre As String <- "Leo";` creates a box that already has a value.
3. Step 3: `libros <- 3;` fills the first box.
4. Step 4: `cuadernos <- libros * 2;` reads `libros` and stores 6.

An empty box **has no default value**: it is not 0 nor `""`. Before running, the engine checks that a variable that may be empty is never read.

Every variable declared with `Define` can be changed later. For a value that never changes there is [`Constant`](#declaraciones.constante).

> **Common mistakes**
>
> The initial value is only valid with one name. With several, it is unclear which one it refers to:
>
> ### Program: Initial value with two names
>
> ```pseudolearn
> Algorithm Varios
>   Define a, b As Integer <- 0;
>   Write a;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `initializerWithMultipleNames` (error, 2:26): An initial value is only allowed when declaring a single name
>
> *The program finishes with an error.*
>
> A name can only be declared once in the same scope:
>
> ### Program: A name declared twice
>
> ```pseudolearn
> Algorithm Repetido
>   Define x As Integer <- 1;
>   Define x As Integer <- 2;
>   Write x;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `duplicateVariableDeclaration` (error, 3:10): Duplicate declaration of variable x in the same scope
>
> *The program finishes with an error.*

> **Under the hood**
>
> Every local variable takes a numbered slot in the frame of its subprogram. In the intermediate representation, the declaration produces no instruction by itself: only the initial value produces a `store`.
>
> **Intermediate representation**
>
> ```text
> module Mochila en
> func suspendible @main() -> unit {
> b0() mass 5:
>   stmt.enter @2:3-2:38
>   %0 = value.undefined i64 @2:3-2:38
>   %1 = value.undefined i64 @2:3-2:38
>   stmt.exit @2:3-2:38
>   stmt.enter @3:3-3:35
>   %2 = const.str "Leo" @3:30-3:35
>   stmt.exit @3:3-3:35
>   stmt.enter @4:3-4:14
>   %3 = const.i64 3 @4:13-4:14
>   stmt.exit @4:3-4:14
>   stmt.enter @5:3-5:26
>   %4 = const.i64 2 @5:25-5:26
>   %5 = int.mul %3, %4 @5:23-5:24
>   stmt.exit @5:3-5:26
>   stmt.enter @6:3-6:73
>   %6 = const.str " lleva " @6:17-6:26
>   %7 = const.str " libros y " @6:36-6:48
>   %8 = const.str " cuadernos" @6:61-6:73
>   out.write %2, %6, %3, %7, %5, %8, newline @6:3-6:73
>   stmt.exit @6:3-6:73
>   return @7:1-7:13
> }
> ```

> **Note**
>
> Before running it, draw the boxes of `mochila`. If you draw `cuadernos` with a 0 from the start, you are assuming a default value that does not exist.

**In short**
- `Define` creates one or more variables of a type.
- The initial value is optional and only valid with one name.
- Without an initial value, the variable starts empty, not at zero.
- A name is not repeated in the same scope.

**See also**
- [5.2 Fixed values with Constant](https://pseudolearn.app/en/spec/declaraciones/constante.md)
- [5.3 Assignment](https://pseudolearn.app/en/spec/declaraciones/asignacion.md)
- [5.5 Variables without a value](https://pseudolearn.app/en/spec/declaraciones/sin-valor.md)
