# 7.5 Returning a value with Return

> `Return` ends the subprogram on the spot and, if it has a return type, hands the value to the caller.

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

> **In one sentence**
>
> `Return` ends the subprogram on the spot and, if it has a return type, hands the value to the caller.

## 7.5.1 How to write it

```pseudolearn
Return ⟦«expresión»⟧;
```

> **Parts**
>
> - `Return` ends the call.
> - «expression» is required if the subprogram declares `As` type, and forbidden otherwise.
> - In a `Function` with a result variable, `Return` goes **without** an expression: it returns the variable.

## 7.5.2 How it works

`Return` cuts everything: it leaves open loops and blocks and abandons the whole call. Whatever comes after does not run. That is why it is useful for leaving as soon as the answer is known:

### Program: Leaving as soon as you know

```pseudolearn
Subroutine PrimerNegativo(a As Integer, b As Integer, c As Integer) As Integer
  If a < 0 Then
    Return a;
  EndIf
  If b < 0 Then
    Return b;
  EndIf
  Return c;
EndSubroutine

Algorithm Principal
  Write PrimerNegativo(4, -2, -7);
EndAlgorithm
```

**Output**

```text
-2
```

*The program finishes normally.*

**Figure 7.5-1: Several exits**

Three Returns. The first If is false; the second holds and b, which is -2, is returned. The last Return is never reached.

Three possible exits. With 4, -2 and -7 the second one is taken, and the third is never reached.

Every path of a typed subprogram must end in a `Return`. If the engine finds none at all, it is an error before running. If there is one, but a particular path reaches the end without going through it, the failure is discovered **when running** that path.

> **Common mistakes**
>
> ### Program: A path without Return
>
> ```pseudolearn
> Subroutine Signo(n As Integer) As String
>   If n > 0 Then
>     Return "positivo";
>   EndIf
> EndSubroutine
>
> Algorithm Principal
>   Write Signo(5);
>   Write Signo(-1);
> EndAlgorithm
> ```
>
> **Output**
>
> ```text
> positivo
> ```
>
> **Diagnostics**
> - `subroutineEndedWithoutReturn` (error, 5:1): An execution path of Signo reaches the end without returning
>
> *The program finishes with an error.*
>
> The first call works; the second reaches `EndSubroutine` with no value to return.

> **Under the hood**
>
> Every `Return` is a `return` instruction that ends the block. A path without `Return` ends in a trap instruction with the position of the closing word.
>
> **Intermediate representation**
>
> ```text
> module Principal en
> func @Signo(%0: i64) -> str {
> b0() mass 1:
>   stmt.enter @2:3-4:8
>   %1 = const.i64 0 @2:10-2:11
>   %2 = int.cmp gt, %0, %1 @2:8-2:9
>   decide.bool %2 @2:3-4:8
>   branch %2, b1(), b2()
> b1() mass 1:
>   stmt.enter @3:5-3:22
>   %3 = const.str "positivo" @3:12-3:22
>   stmt.exit @3:5-3:22
>   return %3 @3:5-3:22
> b2() mass 0:
>   jump b3()
> b3() mass 0:
>   stmt.exit @2:3-4:8
>   trap.missing_return @5:1-5:14
> }
> func suspendible @main() -> unit {
> b0() mass 2:
>   stmt.enter @8:3-8:17
>   %0 = const.i64 5 @8:15-8:16
>   %1 = call @Signo(%0) @8:9-8:17
>   out.write %1, newline @8:3-8:17
>   stmt.exit @8:3-8:17
>   stmt.enter @9:3-9:18
>   %2 = const.i64 1 @9:16-9:17
>   %3 = int.neg %2 @9:15-9:16
>   %4 = call @Signo(%3) @9:9-9:18
>   out.write %4, newline @9:3-9:18
>   stmt.exit @9:3-9:18
>   return @10:1-10:13
> }
> ```

**In short**
- `Return` ends the call on the spot, also from inside loops.
- It takes an expression if and only if the subprogram has a return type.
- A path that reaches the end without returning fails when running.

**See also**
- [7.1 Declaring subprograms](https://pseudolearn.app/en/spec/subprogramas/declaracion.md)
- [6.7 Leaving a loop](https://pseudolearn.app/en/spec/control/salir.md)
- [6.8 Cleanups with Defer](https://pseudolearn.app/en/spec/control/diferir.md)
