# 8.1 Structure of a program

> A program is a text with **a single algorithm** and, around it, the pieces that algorithm uses: subprograms, classes, records, enumerations and constants.

Web page: https://pseudolearn.app/en/spec/programas/estructura

> **In one sentence**
>
> A program is a text with **a single algorithm** and, around it, the pieces that algorithm uses: subprograms, classes, records, enumerations and constants.

## 8.1.1 How it works

The top level of the text only accepts **units**: named blocks that open and close. No executable statement may be left loose outside them.

**Figure 8.1-1: A program file**

A file with four sibling units one under the other: the constant SALUDO, the enumeration Color, the algorithm Tienda, highlighted as the only one, and the subroutine Precio.

A file with its single algorithm and its sibling units. None goes inside another, and order does not matter.

| Unit | How many |
| --- | --- |
| `Algorithm` | exactly one |
| `Subroutine`, `Function` | as many as needed |
| `Class`, `Interface` | as many as needed |
| `Record`, `Enumeration`, `Variant` | as many as needed |
| `Constant` | as many as needed |

Units are **siblings**: none goes inside another. They can be written before or after the algorithm, in any order.

### Program: An algorithm with its neighbours

```pseudolearn
Constant SALUDO As String <- "Hola";

Enum Color
  Rojo, Verde
EndEnum

Algorithm Tienda
  Write SALUDO, " ", Precio(3);
EndAlgorithm

Subroutine Precio(unidades As Integer) As Integer
  Return unidades * 5;
EndSubroutine
```

**Output**

```text
Hola 15
```

*The program finishes normally.*

An empty algorithm is valid and does nothing. The algorithm's name does not take up the variable namespace: it may match the name of a variable.

## 8.1.2 Classic pseudocode

A program written in the classic style —`Algorithm`, `Define` with several names, `Read`, `Write`, `If`, `Switch`, loops, `Dimension` and `Function` with a result variable— is analysed **without any diagnostic** and runs as is, also from the terminal with `pseudo ejecutar`. The only requirements are the usual ones: semicolons, indices from zero and no variable read without a value.

> **Common mistakes**
>
> A statement outside every unit belongs nowhere:
>
> ### Program: A loose statement
>
> ```pseudolearn
> Write "suelta";
> Algorithm Uno
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `unexpectedTokenOutsideProgramUnit` (error, 1:1): Unexpected token outside algorithm and subroutines: Write
>
> *The program finishes with an error.*
>
> And a text cannot have two algorithms:
>
> ### Program: Two algorithms in one text
>
> ```pseudolearn
> Algorithm Uno
> EndAlgorithm
> Algorithm Dos
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `multipleAlgorithms` (error, 3:1): Multiple algorithms in a single source file are not allowed
>
> *The program finishes with an error.*

**In short**
- A text has exactly one algorithm.
- Sibling units go around it, in any order.
- There are no loose statements or nested units.
- Classic pseudocode works without touch-ups.

**See also**
- [8.2 Modules and projects](https://pseudolearn.app/en/spec/programas/modulos.md)
- [7.1 Declaring subprograms](https://pseudolearn.app/en/spec/subprogramas/declaracion.md)
- [1.1 What PseudoLearn is](https://pseudolearn.app/en/spec/vision/que-es.md)
