Skip to content

SpecificationV · Declarations and statements

5.2

Fixed values with Constant

A Constant names a value that never changes, and the engine computes it before running.

Markdown version

5.2.1 How to write it

Constant «NOMBRE» As «tipo» <- «valor»;

5.2.2 How it works

The engine computes the value of every constant while analysing the program. Using it is the same as writing its value: it takes no box in memory and cannot be changed.

ProgramConstants of an invoice

Constant IVA As Real <- 0.21;
Constant SEMANA As Integer <- 7 * 24;
Algorithm Factura
  Constant SALUDO As String <- "Total: ";
  Define base As Real <- 100.0;
  Write SALUDO, base * (1 + IVA), " (", SEMANA, " horas)";
EndAlgorithm

Output

Total: 121.0 (168 horas)

The program finishes normally.

  1. 1The program as written.

  2. 2During analysis, SEMANA is computed once: 7 * 24 = 168.

  3. 3Each use of the constant becomes its value. The variable base does take memory.

Figure 5.2-1.The engine replaces each constant with its value before running. Only variables remain in memory.

A declaration of the constant SEMANA as 7 * 24 that is computed as 168. The line Write base, SEMANA becomes Write base, 168. Only base appears in memory; the constant has no box.

The value is computed with a subset of expressions: literals, other constants, enumeration members, parentheses, signs, Not, + - * / div mod, comparisons, And, Or and concatenation. Variables, function calls and power are not allowed.

In short

  • Constant names a fixed value with a required initializer.
  • It is computed during analysis, with a subset of operations.
  • It takes no memory and cannot be assigned.
  • It can be declared in the module or inside a body.