Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 36 ยท Control Flow and Procedures

Parameters in Pascal: Value, var, const and out

Understand how Pascal passes arguments: by value, by reference with var, read-only const, output-only out, open array parameters, untyped parameters and procedural types.

A parameter is how a routine receives data from its caller. Pascal gives you several parameter modes, and the mode you choose says exactly what the routine may do with the argument. Choosing well makes code safer (a const parameter cannot be changed by accident) and faster (large values are not copied needlessly).

The listings are read-only Pascal. The runnable demos are C stand-ins that show the same ideas using pointers.

Value parameters

By default a parameter is passed by value: the routine gets its own copy. Changing it inside does not affect the caller.

PASCAL
program ByValue;

procedure AddTen(n: Integer);
begin
  n := n + 10;
  WriteLn('inside: ', n);
end;

var
  x: Integer;
begin
  x := 5;
  AddTen(x);
  WriteLn('outside: ', x);
end.

Output:

TEXT
inside: 15
outside: 5

The variable x is untouched. This is the safest mode and the right default for small values.

var parameters: pass by reference

To let a routine change the caller's variable, add var. The routine then works on the original, not a copy:

PASCAL
procedure Swap(var a, b: Integer);
var
  temp: Integer;
begin
  temp := a;
  a := b;
  b := temp;
end;

var
  p, q: Integer;
begin
  p := 1;
  q := 2;
  Swap(p, q);
  WriteLn(p, ' ', q);     { 2 1 }
end.

With var the argument must be a variable (something with an address). Swap(1, 2) is a compile error, because a literal cannot be changed. Use var parameters when a routine must return more than one result or modify its input.

const parameters

const promises the routine will not change the parameter. The compiler enforces it, and may pass large values by reference instead of copying them:

PASCAL
function CountVowels(const text: string): Integer;
var
  c: Char;
begin
  Result := 0;
  for c in text do
    if c in ['a', 'e', 'i', 'o', 'u'] then
      Inc(Result);
end;

Prefer const for strings, records and arrays you only read. For plain integers it makes no difference, but it documents intent.

out parameters

out is like var but says the value is only output: the routine must assign it, and its initial value is irrelevant (and for managed types such as strings, cleared on entry):

PASCAL
procedure Divide(a, b: Integer; out quotient, remainder: Integer);
begin
  quotient := a div b;
  remainder := a mod b;
end;

var
  q, r: Integer;
begin
  Divide(17, 5, q, r);
  WriteLn(q, ' remainder ', r);    { 3 remainder 2 }
end.

Which mode?

Mode Syntax Callee can change caller's variable? Typical use
Value x: T No (works on a copy) Small inputs
Constant const x: T No (read-only) Large inputs (strings, records)
Variable var x: T Yes, and reads its old value In/out values, Swap
Output out x: T Yes, write only Extra results
flowchart LR
    A["caller variable x"] -->|"value: copy"| B["parameter n"]
    A -->|"var / out: same box"| C["parameter n"]

Figure: value parameters copy the data; var and out parameters share the caller's variable.

Open array parameters

A routine can accept an array of any length with an open array parameter, declared array of T with no bounds:

PASCAL
function Sum(const values: array of Integer): Integer;
var
  i: Integer;
begin
  Result := 0;
  for i := Low(values) to High(values) do
    Result := Result + values[i];
end;

begin
  WriteLn(Sum([1, 2, 3, 4]));     { 10 }
end.

Inside, the array is always indexed from 0, so use Low and High (or for ... in). The caller can pass a static array, a dynamic array or an array literal in square brackets. For a variable number of mixed arguments there is also array of const, used by Format.

Untyped parameters and procedural types

An untyped var parameter accepts a variable of any type, which low-level routines such as FillChar and Move use. Avoid it in your own code unless you need it.

A procedural type is a type whose values are routines, so you can pass behavior as a parameter:

PASCAL
type
  TMathFunc = function(x: Integer): Integer;

function Square(x: Integer): Integer;
begin
  Result := x * x;
end;

function Apply(f: TMathFunc; value: Integer): Integer;
begin
  Result := f(value);
end;

begin
  WriteLn(Apply(@Square, 6));     { 36; in Delphi mode write just Square }
end.

In objfpc mode you take the address with @; in delphi mode Apply(Square, 6) is enough.

Parameter pitfalls

  1. Passing a literal or expression to a var parameter.
  2. Forgetting var and wondering why the caller's variable did not change.
  3. Reading an out parameter before assigning it.
  4. Passing the same variable twice to var parameters (aliasing): Swap(x, x) works here, but can surprise in other routines.

Stand-ins in C

C passes everything by value, so "var" is emulated by passing a pointer. The program is C.

C C stand-in: Swap with pointers (var parameters)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C C stand-in: open array and callback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which parameter mode lets a procedure change the caller's variable?

    A var parameter is passed by reference, so assignments inside change the caller's variable. out does too but is output-only.
  2. 2. What is true of a const parameter?

    const parameters are read-only inside the routine and may be passed efficiently without copying.
  3. 3. Why does Swap(1, 2) fail to compile when Swap has var parameters?

    A var parameter needs an assignable variable. A literal has no storage to change.
  4. 4. How are the elements of an open array parameter indexed inside the routine?

    Open arrays are always zero-based inside the routine, whatever the bounds of the argument.

Summary

  • Value parameters copy; var shares the caller's variable; const is read-only; out is output-only.
  • Use const for large read-only data and var/out when a routine must return extra results.
  • Open arrays (array of T) accept any length; index them with Low and High.
  • Procedural types let you pass routines as parameters.

Next: Recursion and Nested Routines