In one sentence
A Constant names a value that never changes, and the engine computes it before running.
5.2.1 How to write itConstant «NOMBRE» As «tipo» <- «valor» ; Parts
Constant opens the declaration. It can go outside every algorithm, in the module, or inside a body.«NAME» is an identifier. Writing it in upper case is a habit, not a rule.«type» is Integer , Real , Boolean , Character , String or an enumeration.<- and the «value» are required.5.2.2 How it worksThe 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.
Program Constants 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.
The constant is replaced 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. Constant SEMANA As Integer <- 7 * 24; Write base, SEMANA;
1 The program as written.
The constant is replaced 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. Constant SEMANA As Integer <- 7 * 24; Write base, SEMANA; Constant SEMANA As Integer <- 168;
2 During analysis, SEMANA is computed once: 7 * 24 = 168.
The constant is replaced 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. Constant SEMANA As Integer <- 7 * 24; Write base, SEMANA; Constant SEMANA As Integer <- 168; Write base, 168; memory 100.0 base constants take no box
3 Each 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.
Limit
Integer computation is checked in 64 bits: overflowing is constantArithmeticOverflow and dividing by zero is constantDivisionByZero , both before running. The computation has a budget of 10000 steps and of 65536 bytes per text. A text of exactly 65536 bytes fits. A constant that depends on itself, directly or indirectly, is circularConstantDefinition . Common mistakes
A constant cannot be assigned:
Program Trying to change a constant Constant LIMITE As Integer <- 10 ;
Algorithm Cambiar
LIMITE <- 20 ;
Write LIMITE ;
EndAlgorithm Diagnostics
constantReassigned · error · 3:3 · Constant LIMITE cannot be assignedThe program finishes with an error.
And its value must be computable without running anything:
Program A power in a constant Constant POT As Integer <- 2 ^ 3 ;
Algorithm Potencia
Write POT ;
EndAlgorithm Diagnostics
constantNotEvaluable · error · 1:10 · The value of constant POT cannot be computed when the program is analyzedThe program finishes with an error.
Under the hood
In the intermediate representation there is no trace of SEMANA or IVA : 168 and 0.21 appear directly as instruction constants instead.
Intermediate representation
module Factura en
func suspendible @main() -> unit {
b0() mass 2:
stmt.enter @5:3-5:31
%0 = const.f64 100.0 @5:26-5:31
stmt.exit @5:3-5:31
stmt.enter @6:3-6:58
%1 = const.str "Total: " @6:9-6:15
%2 = const.i64 1 @6:25-6:26
%3 = int.to_real %2 @6:25-6:26
%4 = const.f64 0.21 @6:29-6:32
%5 = real.add %3, %4 @6:27-6:28
%6 = real.mul %0, %5 @6:22-6:23
%7 = const.str " (" @6:35-6:39
%8 = const.i64 168 @6:41-6:47
%9 = const.str " horas)" @6:49-6:58
out.write %1, %6, %7, %8, %9, newline @6:3-6:58
stmt.exit @6:3-6:58
return @7:1-7:13
} 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. See also