# 7.6 Scopes and names

> Every name exists only inside its **scope**: from where it is declared to the end of the block that contains it.

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

> **In one sentence**
>
> Every name exists only inside its **scope**: from where it is declared to the end of the block that contains it.

## 7.6.1 How it works

PseudoLearn has no global variables. Every algorithm, subprogram and method has its own scope, and inside it every branch of `If` or `Switch` and every loop body opens a smaller scope.

**Figure 7.6-1: Nested scopes**

A large box for the process with the variable n, and inside it another for the If branch with the variable doble. From inside n is visible; from outside doble is not.

Scopes nest like boxes. From inside you can see what is outside; from outside you cannot see what is inside.

A subprogram does not see the variables of the algorithm that calls it. If it needs them, they must be passed as [parameters](https://pseudolearn.app/en/spec/subprogramas/parametros.md):

### Program: A subprogram does not see another one's variables

```pseudolearn
Subroutine VerTotal()
  Write total;
EndSubroutine

Algorithm Principal
  Define total As Integer <- 10;
  VerTotal();
EndAlgorithm
```

**Diagnostics**
- `undeclaredVariable` (error, 2:9): Undeclared variable: total

*The program finishes with an error.*

A variable declared inside a block stops existing when the block closes:

### Program: Using a variable outside its block

```pseudolearn
Algorithm FueraAmbito
  Define n As Integer <- 3;
  If n > 0 Then
    Define doble As Integer <- n * 2;
    Write doble;
  EndIf
  Write doble;
EndAlgorithm
```

**Diagnostics**
- `bindingOutsideScope` (error, 7:9): Declaration of doble is outside its scope

*The program finishes with an error.*

Declaring inside a name that already exists outside is valid, but it **shadows** the outer one while the block lasts, and produces a warning:

### Program: A name that shadows another

```pseudolearn
Algorithm Ambitos
  Define x As Integer <- 1;
  If x > 0 Then
    Define x As Integer <- 2;
    Write "dentro: ", x;
  EndIf
  Write "fuera: ", x;
EndAlgorithm
```

**Output**

```text
dentro: 2
fuera: 1
```

**Diagnostics**
- `bindingShadowsOuter` (warning, 4:12): Declaration of x shadows an outer binding

*The program finishes normally.*

> **Note**
>
> The names of subprograms and classes are registered before their bodies. That is why a subprogram can call another one declared further down, and two subprograms can call each other.

**In short**
- There are no global variables.
- A name lives from its declaration to the end of its block.
- Shadowing an outer name is valid, with a warning.
- Subprograms can be called in any order.

**See also**
- [7.2 Parameters](https://pseudolearn.app/en/spec/subprogramas/parametros.md)
- [5.1 Declaring variables with Define](https://pseudolearn.app/en/spec/declaraciones/definir.md)
