ZetCode

Algol 68 variables and references

last modified November 13, 2026

Algol 68 separates values from the places that hold them. An identity declaration binds a name to a value, while a variable declaration creates a reference to a freshly allocated cell. Understanding that difference makes references, aliases, and the LOC and HEAP generators easy to read.

Constants and variables

A declaration with = is an identity declaration: the name becomes another name for the value and cannot be reassigned. A declaration with := is a variable declaration: it allocates a cell, stores the initial value, and lets the program assign new values later.

varconst.a68
BEGIN
  INT variable := 10;
  INT constant = 5;
  variable := variable + 1;
  print((whole(variable, 0), " ", whole(constant, 0), newline))
END

The assignment on the third line changes variable to 11. Assigning to constant would be rejected by the compiler, because an identity declaration does not introduce a variable. The := form is shorthand for a reference declaration with a local generator.

$ a68g varconst.a68
11 5

Assignment operators

Every dyadic operator can be combined with assignment. The operator is applied to the current value and the right-hand side, and the result is stored back. This works for integers, reals, and other numeric modes.

assignops.a68
BEGIN
  INT n := 10;
  n +:= 5;
  n -:= 3;
  n *:= 2;
  print((whole(n, 0), newline));
  REAL r := 10.0;
  r /:= 4.0;
  print((fixed(r, 0, 2), newline))
END

The integer passes through 15, 12, and 24. The real is divided by 4 and printed as 2.50. The combined forms avoid repeating the variable name and make the update stand out.

$ a68g assignops.a68
24
2.50

References as aliases

A reference names a value stored elsewhere. The mode REF INT means a reference to an integer. An identity declaration of a reference binds the reference once, so the name is a fixed alias for the same cell.

alias.a68
BEGIN
  INT x := 10;
  REF INT r = x;
  r := 30;
  print((whole(x, 0), newline))
END

r is not a copy of x; it refers to the same cell. Assigning through r therefore changes x, and the program prints 30. The alias itself cannot be pointed at another cell, because it was declared with =.

$ a68g alias.a68
30

Reference variables

A reference may also be a variable. Declaring REF INT p := x creates a reference variable that initially designates x. Assigning another reference to p changes which cell it designates, without touching the value in the old cell.

refvar.a68
BEGIN
  INT x := 10, y := 20;
  REF INT p := x;
  p := y;
  print((whole(p, 0), newline))
END

After p := y, reading p produces the value of y, namely 20. The cell that held 10 is unchanged, but p no longer refers to it. This is the Algol 68 equivalent of reassigning a pointer.

$ a68g refvar.a68
20

Local and heap generators

LOC generates a value in the current stack frame, and HEAP generates one on the heap. Both generators yield a reference, so they are normally used with an identity declaration. Heap values are reclaimed by the garbage collector when they are no longer reachable.

generators.a68
BEGIN
  REF INT local = LOC INT := 7;
  REF INT heap = HEAP INT := 12;
  local := 9;
  heap := 13;
  print((whole(local, 0), " ", whole(heap, 0), newline))
END

Both names are fixed aliases to generated cells. The assignments store new values in those cells, and the final line reads them back. A local cell lives only as long as the clause that created it, while a heap cell can outlive that clause as long as a reference to it exists.

$ a68g generators.a68
9 13

NIL and identity

A reference can be NIL, meaning that it designates nothing. NIL is useful for empty lists and optional links. The operators IS and ISNT test identity, so a reference can be compared with NIL before it is dereferenced.

nil.a68
BEGIN
  REF INT p = NIL;
  print((p IS NIL, newline));
  REF INT q = LOC INT := 5;
  print((q IS NIL, newline))
END

The first reference is NIL and the test prints T. The second designates a generated cell and the test prints F. Testing for NIL before reading a reference prevents a runtime error.

$ a68g nil.a68
T
F

Swapping values

Assignments copy values, so swapping two variables needs a third variable to hold one value temporarily. Algol 68 has no tuple assignment, which means the three assignments must be written explicitly.

swap.a68
BEGIN
  INT a := 1, b := 2;
  INT tmp := a;
  a := b;
  b := tmp;
  print((whole(a, 0), " ", whole(b, 0), newline))
END

tmp saves the old value of a, then a receives the value of b, and finally b receives the saved value. After the swap, a is 2 and b is 1.

$ a68g swap.a68
2 1

Next steps

The modes article describes the values that references can designate, and the program structure article explains the clauses that control where those values live. Algol 68 basics shows declarations and procedures in complete programs.

Source

Revised Report on the Algorithmic Language Algol 68

The Algol 68 Genie project

In this article we have distinguished constants from variables, used references as aliases and as reference variables, allocated cells with LOC and HEAP, tested references against NIL, and swapped two values. The modes article provides the background for the values used here.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Algol 68 tutorials.