Skip to content

SpecificationVII · Subroutines

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.

Markdown version

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.Scopes nest like boxes. From inside you can see what is outside; from outside you cannot see what is inside.

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.

A subprogram does not see the variables of the algorithm that calls it. If it needs them, they must be passed as parameters:

ProgramA subprogram does not see another one's variables

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:

ProgramUsing a variable outside its block

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:

ProgramA name that shadows another

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

dentro: 2
fuera: 1

Diagnostics

  • bindingShadowsOuter · warning · 4:12 · Declaration of x shadows an outer binding

The program finishes normally.

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.