# 7.1 Declaring subprograms

> A subprogram is a named piece of program: it is written once and used as many times as needed.

Web page: https://pseudolearn.app/en/spec/subprogramas/declaracion

> **In one sentence**
>
> A subprogram is a named piece of program: it is written once and used as many times as needed.

## 7.1.1 How to write it

```pseudolearn
Subroutine «nombre»(«parámetros») ⟦As «tipo»⟧
  «instrucciones»
EndSubroutine
Function «resultado» <- «nombre»(«parámetros»)
  Define «resultado» As «tipo»;
  «instrucciones»
EndFunction
```

> **Parts**
>
> - `Subroutine` opens a subprogram. The parentheses are required even with no parameters.
> - `As` «type» is optional: with it, the subprogram **returns** a value; without it, it only does things.
> - `Function` declares a function with a **result variable**: the value «result» holds at the end is what is returned.
> - `EndSubroutine` and `EndFunction` close each form.

## 7.1.2 How it works

Subprograms are declared **next to** the main algorithm, never inside it nor inside another subprogram. Order does not matter: you can call one that is written further down.

### Program: A procedure and a function

```pseudolearn
Subroutine Saludar(nombre As String)
  Write "Hola, ", nombre;
EndSubroutine

Subroutine Doble(n As Integer) As Integer
  Return n * 2;
EndSubroutine

Algorithm Principal
  Saludar("Ana");
  Write Doble(21);
EndAlgorithm
```

**Output**

```text
Hola, Ana
42
```

*The program finishes normally.*

**Figure 7.1-1: A subprogram as a box**

A box called Doble. The argument 21 goes in on the left and the result 42 comes out on the right.

A subprogram seen from outside: the arguments go in, the result comes out. What happens inside is hidden.

The `Function` form is handy when the result is built up little by little. It has no `Return` with a value: it returns whatever its result variable holds.

### Program: Function with a result variable

```pseudolearn
Function area <- AreaRectangulo(base As Real, alto As Real)
  Define area As Real;
  area <- base * alto;
EndFunction

Algorithm Principal
  Write AreaRectangulo(3.0, 4.5);
EndAlgorithm
```

**Output**

```text
13.5
```

*The program finishes normally.*

Inside a subprogram everything that works in the algorithm works too: declarations, `Read`, `Write`, decisions and loops. The variables it declares are **local**: nobody else sees them.

> **Limit**
>
> - The return type can be a primitive or a class, never an array.
> - A `Function` is [pure](https://pseudolearn.app/en/spec/subprogramas/pureza.md): it cannot write, read or call anything that does.

> **Common mistakes**
>
> A subprogram with a return type needs at least one `Return`:
>
> ### Program: A return type without Return
>
> ```pseudolearn
> Subroutine Siete() As Integer
>   Write "sin retorno";
> EndSubroutine
>
> Algorithm Principal
>   Write Siete();
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `subroutineWithoutReturn` (error, 1:1): Subroutine Siete declares a return type but its body contains no return statement
>
> *The program finishes with an error.*

> **Under the hood**
>
> Every subprogram is a function of the intermediate representation with its own blocks. A call is a `call` instruction that names the target function.
>
> **Intermediate representation**
>
> ```text
> module Principal en
> func @Saludar(%0: str) -> unit {
> b0() mass 1:
>   stmt.enter @2:3-2:25
>   %1 = const.str "Hola, " @2:9-2:17
>   out.write %1, %0, newline @2:3-2:25
>   stmt.exit @2:3-2:25
>   return @3:1-3:14
> }
> func @Doble(%0: i64) -> i64 {
> b0() mass 1:
>   stmt.enter @6:3-6:15
>   %1 = const.i64 2 @6:14-6:15
>   %2 = int.mul %0, %1 @6:12-6:13
>   stmt.exit @6:3-6:15
>   return %2 @6:3-6:15
> }
> func suspendible @main() -> unit {
> b0() mass 2:
>   stmt.enter @10:3-10:17
>   %0 = const.str "Ana" @10:11-10:16
>   %1 = call @Saludar(%0) @10:3-10:17
>   stmt.exit @10:3-10:17
>   stmt.enter @11:3-11:18
>   %2 = const.i64 21 @11:15-11:17
>   %3 = call @Doble(%2) @11:9-11:18
>   out.write %3, newline @11:3-11:18
>   stmt.exit @11:3-11:18
>   return @12:1-12:13
> }
> ```

> **Note**
>
> Before writing subprograms, find two almost identical pieces in a long program. That is the first candidate for a subprogram.

**In short**
- `Subroutine` declares a subprogram; with `As` type it returns a value.
- `Function` returns what its result variable holds and is pure.
- They are declared next to the algorithm, in any order.
- Their variables are local.

**See also**
- [7.2 Parameters](https://pseudolearn.app/en/spec/subprogramas/parametros.md)
- [7.4 Calls](https://pseudolearn.app/en/spec/subprogramas/llamada.md)
- [7.5 Returning a value with Return](https://pseudolearn.app/en/spec/subprogramas/retornar.md)
