HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to DelphiVariablesData Types & ConversionOperatorsStringsConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔄 Data Types & Conversion
✍️ String
🔁 Condition & Loop
🧩 Function & Procedure
Delphi›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
{$MODE DELPHI}
3
function square(x: Integer): Integer;
Return type : Integer.
FunctionReturn type : Integer.
4
begin
5
square := x * x;
Assign result to the NAME.
FunctionAssign result to the NAME.
6
end;
7
begin
Main program.
DefinitionMain program.
8
writeln(square(5));
25
Output / Input25
9
end.
FunctionDefinitionOutput / Input
💡 Hint: Return: assigned to the function name (square := x*x) or Result is used.

About this topic

Subprograms that return a value in Delphi are called functions, and those that do not are called procedures. In functions, the return value is assigned to a special name; this name is implicitly defined inside every function.

Parameters are copied by default. To modify them, a variable parameter is used; to only read and avoid copying, a constant parameter is used; and to only return a result, an output parameter is used. These four forms document the intent in the signature.

Delphi supports overloading, default parameters, and anonymous methods. These shorten the code, but when overloaded signatures remain ambiguous, the compiler cannot decide which version to choose and throws an error.

What you will learn

  • The distinction between functions and procedures
  • Assigning a return value
  • The four parameter forms
  • Default parameters
  • Overloading

Who is this for?

  • Those who divide their code into subprograms
  • Those that cannot receive a return value
  • Those expecting parameter modifications

When to use?

Using a constant parameter modifier when passing large records and texts as parameters prevents unnecessary copying.

Default parameters are a clean way to consolidate many overloads that perform the same task at different levels of detail into a single signature.

Common mistakes

Wrong

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

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

The function declared it would return an integer, but no return value was assigned; printing is not returning. The result becomes undefined and the compiler only gives a warning. A return value must be assigned on every function path.

Correct

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

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

Wrong

function Siniflandir(puan: Integer): string;
begin
  if puan >= 50 then
    Result := 'passed';
end;

begin
  writeln(Siniflandir(30));
end.

When the condition is not met, no return value is assigned; since the text type is managed, it returns empty, but numeric types yield garbage data. A return value must be assigned on every possible path or a default value must be provided at the beginning of the function.

Correct

function Siniflandir(puan: Integer): string;
begin
  Result := 'failed';
  if puan >= 50 then
    Result := 'passed';
end;

begin
  writeln(Siniflandir(30));
end.

Wrong

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

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

Since the parameter is copied, the procedure only modifies its own copy; the external value does not change. If you want to modify the caller's variable, you must mark the parameter as a variable parameter.

Correct

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

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

Frequently asked questions

Can I also assign the return value using the function name?

Yes, for Pascal compatibility, assignment to the function name also works. However, the special return name is clearer, and since it behaves like a variable while being readable, it also allows accumulation on it.

How do I choose between a constant and a variable parameter?

If you are going to modify the value, use a variable parameter; if you are only going to read it and the type is large, use a constant parameter. For small numeric types, there is no difference; for large records and texts, a constant parameter provides a noticeable speed gain.

Where are default parameters written?

At the end of the signature. A parameter without a default value cannot be written after a parameter that has one; the compiler will reject it. In the call, values cannot be skipped either; they must be omitted starting from the end.

Summary

The return value is assigned to a special name.

A return value must be assigned on every path.

Parameters are copied by default.

A variable parameter is required for modification.

For large types, a constant parameter prevents copying.

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