ZetCode

Pascal procedures and functions

last modified November 18, 2026 Free Pascal 3.2.2

A procedure performs an action, and a function computes a value. Both let a program name a piece of code, give it parameters, and call it from several places. This article shows how procedures and functions are declared and called, how the parameter modes differ, how default and open-array parameters work, and how overloading and forward declarations fit together.

A simple procedure

A procedure is declared with the keyword procedure, a name, an optional parameter list, and a body between begin and end. A procedure with no parameters is called by its name alone, without parentheses. The declaration must appear before the first call.

simpleproc.pas
program simpleproc;
{$mode objfpc}{$H+}

procedure Hello;
begin
  WriteLn('Hello from a procedure');
end;

begin
  Hello;
  Hello;
end.

The procedure is declared at the top level and called twice from the main block. The semicolon after end belongs to the declaration; the call itself is a statement and ends with a semicolon. A procedure that has no parameters still has an empty parameter list in its declaration, but the parentheses are omitted when the list is empty.

$ fpc -gl simpleproc.pas
$ ./simpleproc
Hello from a procedure
Hello from a procedure

Functions and Result

A function is declared like a procedure, but its header ends with a colon and the type of the value it returns. Inside the body, the predefined variable Result holds the return value. Assigning to Result is the usual way to return a value; assigning to the function name also works for compatibility with older Pascal code.

functions.pas
program functions;
{$mode objfpc}{$H+}

function Add(a, b: Integer): Integer;
begin
  Result := a + b;
end;

function MaxOf(a, b: Integer): Integer;
begin
  if a > b then
    Result := a
  else
    Result := b;
end;

begin
  WriteLn('2 + 3 = ', Add(2, 3));
  WriteLn('max(7, 4) = ', MaxOf(7, 4));
end.

Add returns the sum of its two parameters, and MaxOf returns the larger one. A function call is an expression, so it can be used inside WriteLn, an assignment, or another expression. A procedure call is a statement and cannot be used as a value.

$ fpc -gl functions.pas
$ ./functions
2 + 3 = 5
max(7, 4) = 7

Value parameters

Parameters written without a keyword are value parameters. The caller's value is copied into the parameter, and the procedure works on the copy. Assigning to a value parameter does not change the caller's variable, which makes the procedure safe to call with constants and expressions.

valueparam.pas
program valueparam;
{$mode objfpc}{$H+}

procedure TryChange(x: Integer);
begin
  x := 99;
  WriteLn('inside:  ', x);
end;

var
  n: Integer;

begin
  n := 5;
  TryChange(n);
  WriteLn('outside: ', n);
end.

The assignment inside the procedure changes only the local copy, so the caller's n remains 5. Value parameters are the default because they are easy to reason about: a call cannot modify the argument unless the parameter is explicitly declared as var or out.

$ fpc -gl valueparam.pas
$ ./valueparam
inside:  99
outside: 5

Var parameters

A var parameter is passed by reference. The procedure receives the address of the caller's variable and can change it. The argument must be a variable, not a literal or an expression, because there must be a storage location to modify. A var parameter is also the way to return more than one value from a procedure.

varparam.pas
program varparam;
{$mode objfpc}{$H+}

procedure Double(var n: Integer);
begin
  n := n * 2;
end;

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

var
  value, x, y: Integer;

begin
  value := 7;
  Double(value);
  WriteLn('value = ', value);
  x := 3;
  y := 8;
  Swap(x, y);
  WriteLn('x = ', x, ', y = ', y);
end.

Double(value) changes the caller's variable from 7 to 14, and Swap(x, y) exchanges the two values. The temporary variable in Swap is local to the procedure. The same effect can be written for a single variable with Inc, but the var form is general and works for any type.

$ fpc -gl varparam.pas
$ ./varparam
value = 14
x = 8, y = 3

Out parameters

An out parameter is like a var parameter, except that the procedure is expected to assign it before returning. The compiler may treat the parameter as uninitialised on entry, so reading it before assigning is a mistake. out documents that a parameter carries a result rather than an input that is also modified.

outparam.pas
program outparam;
{$mode objfpc}{$H+}

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('17 div 5 = ', q, ', remainder ', r);
end.

The procedure returns two results through quotient and remainder. Both are assigned before the procedure returns, so the caller can use them immediately. A function can return only one value, so an out parameter is the natural way to return a pair.

$ fpc -gl outparam.pas
$ ./outparam
17 div 5 = 3, remainder 2

Const parameters

A const parameter is read-only inside the procedure. The compiler may pass it by reference to avoid copying a large string or record, but it forbids assignment. This is the preferred mode for input parameters that the procedure does not need to modify, especially when the type is large.

constparam.pas
program constparam;
{$mode objfpc}{$H+}

procedure ShowLength(const s: string);
begin
  WriteLn(s, ' has ', Length(s), ' bytes');
end;

var
  name: string;

begin
  name := 'Pascal';
  ShowLength(name);
  ShowLength('Free Pascal');
end.

ShowLength accepts both a variable and a literal, because a const parameter can be read but not changed. Trying to assign to s inside the procedure would be a compile-time error. The length is in bytes; for ASCII text that is also the number of characters.

$ fpc -gl constparam.pas
$ ./constparam
Pascal has 6 bytes
Free Pascal has 11 bytes

Default parameters

A parameter can have a default value, written after its type with an equals sign. The caller may omit the argument, in which case the default is used. Default parameters must come after all parameters without defaults, and the default value must be a constant expression.

defaults.pas
program defaults;
{$mode objfpc}{$H+}

procedure Greet(const name: string; times: Integer = 1);
var
  i: Integer;
begin
  for i := 1 to times do
    WriteLn('Hello, ', name);
end;

begin
  Greet('Alice');
  Greet('Bob', 2);
end.

The first call uses the default times = 1 and prints one line. The second call supplies 2 and prints two lines. A caller that wants to pass a later parameter must also pass the earlier ones, because Pascal has no named arguments. Defaults make a procedure convenient to call in the common case without giving up the general case.

$ fpc -gl defaults.pas
$ ./defaults
Hello, Alice
Hello, Bob
Hello, Bob

Open array parameters

An open array parameter accepts an array of any length. It is declared as array of T without bounds, and inside the procedure Low, High, and Length describe the actual argument. The caller can pass a static array, a dynamic array, or an array constructor written in square brackets.

openarray.pas
program openarray;
{$mode objfpc}{$H+}

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 = ', Sum([1, 2, 3, 4, 5]));
end.

The function receives the five values as an open array. Low is 0 and High is 4 for this argument, regardless of how the caller declared its own array. The const modifier prevents the function from changing the elements. The arrays article explains the array types in detail.

$ fpc -gl openarray.pas
$ ./openarray
Sum = 15

Overloading

Several functions or procedures can share a name if their parameter lists differ. This is called overloading. Each declaration carries the overload directive, and the compiler chooses the version whose parameters match the call. The return type alone is not enough to distinguish two overloads.

overload.pas
program overload;
{$mode objfpc}{$H+}

uses Math;

function Area(r: Double): Double; overload;
begin
  Result := Pi * Sqr(r);
end;

function Area(w, h: Double): Double; overload;
begin
  Result := w * h;
end;

begin
  WriteLn('circle: ', Area(2.0):0:2);
  WriteLn('rectangle: ', Area(3.0, 4.0):0:2);
end.

The first Area takes one argument and computes the area of a circle; the second takes two and computes the area of a rectangle. The call Area(2.0) matches the first, and Area(3.0, 4.0) matches the second. If the arguments are integers, the compiler can convert them to Double, but a call that matches both versions exactly is preferred.

$ fpc -gl overload.pas
$ ./overload
circle: 12.57
rectangle: 12.00

Forward declarations

A procedure must be declared before it is called. When two procedures call each other, neither can be written completely before the other. The solution is a forward declaration: the header of the first procedure is written with the forward directive, and its body follows later.

forward.pas
program forward;
{$mode objfpc}{$H+}

procedure Second; forward;

procedure First;
begin
  WriteLn('first calls second');
  Second;
end;

procedure Second;
begin
  WriteLn('second');
end;

begin
  First;
end.

The forward declaration tells the compiler the name and signature of Second, so First can call it. The real body of Second follows without repeating the parameter list. A forward declaration is also used when a procedure is implemented in a later part of a unit.

$ fpc -gl forward.pas
$ ./forward
first calls second
second

Exit and early return

Exit leaves the current procedure or function immediately. In a function it returns the current value of Result, so assign Result before calling Exit if the function has to return a value. Exit is useful for guard clauses that handle an invalid input at the top of a procedure.

exitproc.pas
program exitproc;
{$mode objfpc}{$H+}

procedure Check(n: Integer);
begin
  if n < 0 then
  begin
    WriteLn(n, ' is negative');
    Exit;
  end;
  WriteLn(n, ' is not negative');
end;

begin
  Check(-5);
  Check(5);
end.

The first call prints the negative message and returns before the second message. The second call skips the guard and prints the normal message. The same pattern replaces a deeply nested if chain and usually makes the procedure easier to read.

$ fpc -gl exitproc.pas
$ ./exitproc
-5 is negative
5 is not negative

Next steps

The units article shows how to put procedures and functions in a separate file that other programs can use. The arrays article explains the array types that open array parameters accept, and the recursion article covers functions that call themselves.

Source

Free Pascal Reference guide

Free Pascal: procedures and functions

Pascal on Wikipedia

In this article we have declared procedures and functions, returned values through Result, used value, var, out, and const parameters, and covered default values, open arrays, overloading, forward declarations, and Exit. The next article in the series covers Pascal units.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.