HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PascalVariablesData Types & ConversionOperatorsStringsConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ String
🔁 Condition & Loop
🧩 Function & Procedure
Pascal›Functions

Functions

function (return value assigned to name) and procedure.

Simple enough for a 5-year-old to understand

function

The return value is assigned to the function's NAME (square := ...). It is defined before begin.

Example code
1
program H;
2
function square(x: integer): integer;
Return type : integer.
FunctionReturn type : integer.
3
begin
4
square := x * x;
Assign result to the NAME.
FunctionAssign result to the NAME.
5
end;
6
begin
Main program.
DefinitionMain program.
7
writeln(square(5));
25
Output / Input25
8
end.
FunctionDefinitionOutput / Input
💡 Hint: The return value is assigned to the function name: square := x*x; (there is no return).

About this topic

Pascal divides subprograms into two: those that return a value are called functions, and those that do not are called procedures. This distinction is part of the language's structural discipline and allows you to read the intent from the signature.

In functions, the return value is given by assigning to the function's own name or, in modern compilers, by using a special name. If no assignment is made, the return value remains undefined and this is not always caught by the compiler.

Parameters are passed by value copy by default: modifying them inside the function does not affect the caller. If you really want to change the outside variable, you must mark the parameter specially.

What you will learn

  • The distinction between functions and procedures
  • Assigning a return value
  • Parameter passing methods
  • Local variables
  • The importance of declaration order

Who is this for?

  • Students learning to divide their programs
  • Those that cannot receive a return value
  • Those expecting parameter modifications

When to use?

If the result of a calculation is needed, a function is used; if only a task is performed, a procedure is used; this distinction is a free way to document the purpose of the code.

When passing large data structures as parameters, a constant parameter marker is used to avoid the cost of copying.

Common mistakes

Wrong

program Test;

function Topla(a, b: Integer): Integer;
begin
  writeln(a + b);
end;

begin
  writeln(Topla(2, 3));
end.

The function declared that it would return an integer, but no return value was assigned; printing to the screen does not return a value. The result becomes an undefined value and some compilers only give a warning. Printing and returning are different things.

Correct

program Test;

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

begin
  writeln(Topla(2, 3));
end.

Wrong

program Test;

procedure Artir(sayi: Integer);
begin
  sayi := sayi + 1;
end;

var
  deger: Integer;
begin
  deger := 5;
  Artir(deger);
  writeln(deger);
end.

Since the parameter was passed by value copy, the procedure only modifies its own copy; five is still printed on the screen. If you want to change the caller's variable, you must mark the parameter as a variable parameter.

Correct

program Test;

procedure Artir(var sayi: Integer);
begin
  sayi := sayi + 1;
end;

var
  deger: Integer;
begin
  deger := 5;
  Artir(deger);
  writeln(deger);
end.

Wrong

program Test;
begin
  writeln(Topla(2, 3));
end.

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

Pascal processes the source from top to bottom; because the function is not yet known where it is used, an "Identifier not found" error is received. Subprograms must be defined or forward-declared before the main body.

Correct

program Test;

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

begin
  writeln(Topla(2, 3));
end.

Frequently asked questions

When is a variable parameter used?

When you need to modify the caller's variable or pass a large structure without copying it. In the second case, if you are not going to modify it, a constant parameter marker is safer: there is no copying, but modification is also prevented.

What is a forward declaration?

It allows you to introduce the signature of a subprogram in advance and write its body later. It is mandatory when two subprograms call each other; otherwise, whichever one you write first, the other will be unknown.

Where are local variables defined?

In the declaration section between the subprogram's header and body. These variables only exist while that subprogram is running and are not visible from the outside; it is the natural way to prevent name collisions.

Summary

Functions return a value, procedures do not.

The return value is assigned to the function name.

Parameters are copied by default.

A variable parameter is required for modification.

Subprograms must be defined before they are used.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code