Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 4: Procedures and Functions

Procedures and functions are blocks of code that perform specific tasks. Organizing a program into them makes the code easier to read and maintain, and Pascal keeps both simple to declare and use.

Procedures

A procedure is a block of code that performs a task but doesn’t return a value.

Declaring procedures

procedure ProcedureName(parameter_list);
begin
  { procedure body }
end;

Example

program ProcedureExample;

procedure Greet(name: string);
begin
  writeln('Hello, ', name);
end;

begin
  Greet('Alice');
end.

The Greet procedure takes a string parameter, name, and prints a greeting.

Functions

A function is like a procedure, except that it returns a value.

Declaring functions

function FunctionName(parameter_list): return_type;
begin
  { function body }
  FunctionName := return_value;
end;

Example

program FunctionExample;

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

begin
  writeln('Sum is: ', Add(5, 10));
end.

The Add function takes two integer parameters, a and b, and returns their sum.

Parameter passing

Pascal gives you more than one way to pass parameters to procedures and functions.

By value

When a parameter is passed by value, the procedure or function receives a copy of the actual parameter. Changing that copy inside the procedure or function has no effect on the actual parameter.

procedure ByValueExample(x: integer);
begin
  x := x + 1;
end;

var
  a: integer;
begin
  a := 5;
  ByValueExample(a);
  writeln('a = ', a);  { Output: a = 5 }
end.

By reference

When a parameter is passed by reference, the procedure or function works on the actual parameter itself, so any change it makes to the parameter changes the actual parameter too.

procedure ByReferenceExample(var x: integer);
begin
  x := x + 1;
end;

var
  a: integer;
begin
  a := 5;
  ByReferenceExample(a);
  writeln('a = ', a);  { Output: a = 6 }
end.

Example: by value and by reference

program ParameterPassingExample;

procedure IncrementByValue(x: integer);
begin
  x := x + 1;
end;

procedure IncrementByReference(var x: integer);
begin
  x := x + 1;
end;

var
  a, b: integer;
begin
  a := 5;
  b := 5;
  
  IncrementByValue(a);
  IncrementByReference(b);
  
  writeln('a (by value) = ', a);  { Output: a (by value) = 5 }
  writeln('b (by reference) = ', b);  { Output: b (by reference) = 6 }
end.

Recursive procedures and functions

Recursion is when a procedure or function calls itself.

Example: factorial calculation

program FactorialExample;

function Factorial(n: integer): integer;
begin
  if n = 0 then
    Factorial := 1
  else
    Factorial := n * Factorial(n - 1);
end;

begin
  writeln('Factorial of 5 is: ', Factorial(5));  { Output: 120 }
end.

To calculate the factorial of a number, the Factorial function calls itself.

Local and global variables

Local variables

A local variable is declared inside a procedure or function, and only that procedure or function can access it.

program LocalVariableExample;

procedure PrintLocalVariable;
var
  localVar: integer;
begin
  localVar := 10;
  writeln('Local Variable: ', localVar);
end;

begin
  PrintLocalVariable;
end.

Global variables

A global variable is declared outside every procedure and function. Any procedure or function in the program can access it.

program GlobalVariableExample;

var
  globalVar: integer;

procedure PrintGlobalVariable;
begin
  globalVar := 10;
  writeln('Global Variable: ', globalVar);
end;

begin
  PrintGlobalVariable;
end.