Passing Parameters

Passing Parameters By Value

By default, when you pass a parameter to a Delphi function Delphi will create a copy of the value in a new variable, and pass that new variable to the function – this means you can change the value of the parameter inside the function, and the value of the variable passed will remain untouched. This is called passing by value.

Passing Parameters By Reference

Passing by value is very useful, but as the computer needs to copy the value before passing it to the method, is slightly less efficient.

const parameters

If you do not need to change the value of a parameter in a delphi method, you can specify the const keyword before the parameter:

The const keyword tells Delphi that you do not want to change the value of the parameter inside the procedure, and passes a reference to the original variable, and as it does not need to create a new variable when calling the procedure, your program will run a little faster.

If you try to change the value of a const parameter in a function, your program will not compile, as Delphi will treat this as an error in your program.

var parameters

If you want to change the value of a variable passed as a parameter, you can use the var declaration with your parameter. This tells Delphi that the method is allowed to change the value of the passed variable:

out parameters

Out parameters are very similar to var parameters (they are still passed by reference), but out tells Delphi that passed variable does not need to have been set before calling the function.

Returning More Than One Value

If you need to return more than one value from a function, you can do this by using out parameters:

In the example above, the procedure accepts two variables as parameters, and the value of each variable is changed by the procedure, resulting in two values being returned:

The message displayed above would be ‘Joe Bloggs is 50’. This is because the values of both varables were set by the function GetName.


Most programming languages allow you to create subroutines usually called functions or methods.  In Delphi there are two types of method, functions and procedures.


Most programming languages call subroutines as functions.  In Delphi a function is a method that will return a result:

The function above will return the value 100 when executed.


To make a function much useful, you can pass additional information to the function using parameters:

In the example above, the function accepts an additional parameter that is passed each time it is called, and the value of this parameter affects the result.

Calling Functions

Once your function has been written, you can start to use it in  your code.

The code sample above shows the function being used to set the value of a variable named TotalPrice, which is then displayed to the user.

Returning a Result

In many other programming languages, you would set the result of a function using the return keyword, however in Delphi you set the value of a special variable called Result.

In the PHP function above the result value would be 100, as calling return will cause the function to exit and return the value specified.  In Delphi things work a little different.

In the Delphi code above, the function would return 200.  This is because setting the result value does not cause the function to exit, and the second statement would be executed.


When running statements within a function, you can instruct Delphi to exit the function using the Exit statement:

In the function above, the result value would be 100.  This is because the Exit statement would  cause the function to be ended before the final statement is executed.


Procedures are a special type of method, that unlike a function does not return a value.


What Is Delphi?

Embarcadero Delphi is an programming tool used to develop programs which can be run on Windows computers (newer versions even allow you to create Mac and iOS programs too).

Delphi uses the Object Pascal language, and generates native code using a compiler – this means it creates .exe files!

One major benefit of using Delphi to develop Windows programs is the Visual Component Library (VCL) – this is a collection of forms and controls (things like input boxes, text labels and buttons), that make it easy to create applications and interfaces quickly and visually using the Delphi Form Designer.

Because the VCL was developed to work well with Windows computers, writing applications for other platforms such as iOS or Max OSX uses a different collection of forms and controls called FireMonkey.


When your program needs to repeat a sequence of instructions you will need to use a loop.  There are three main types of loop in Delphi:

While Loop

A while loop checks a condition at the start of each iteration, and executes the enclosed statements only if the condition is met:

For Loops

For loops in Delphi are a little different to for loops in many other languages, but the basics are still the same.  You will use a for loop to repeat the enclosed statements a set number of times, changing the value of a specified variable with each iteration.

In the example above, the value of i is initially set to 1, and the enclosed statements will be executed.  On the second iteration the value of i is increased to 2, and the enclosed statements are executed.  This will continue until the value of i exceeds 100.

Counting down in a for loop

By default, Delphi for loops will count upwards.  If you need to count backwards in a Delphi for loop you will need to use the downto keyword:

Repeat Until Loops

Repeat until loops are a lot like while loops, but cruicially the condition is evaluated after the first iteration of the loop.

It is also important to note that the loop will stop running if the condition is met, where while loops stop when the condition is not met.

Controlling Loops

Delphi provides two additional statements you can use to add a bit more control to your loops.


To exit a loop, you can use the break statement:


To instantly jump to the next iteration of a loop, you can use the continue statement:

Infinite Loops

An infinite loop is a common programming mistake where a program will enter a loop that will never exit, consider the following:

In the example above, the value of i is set to zero.  We then enter a while loop which will test the value of i is less than 100.  As none of the statements inside the loop will change the value of i, the value of i will always be less than 100, and the loop will never terminate.

IF Statement

When your program needs to make a decision, you need to use the IF statement.  It is common in most programming languages.

An if statement is a special command you can use to test if a ‘condition’ is true, and depending on the condition being met, execute specified statements.

This example above shows a basic if statement:


With an if statement it is also possible to execute alternative statements if the condition is not met, in this case you will need to make use of the else statement:

As you can see above, two sets of statements are specified, one to be executed if the condition is met, another to be executed if the condition is not met.


When specifying conditions for your if statement, you have a large number of options you can use to test whether your condition has been met:

Is equal to ( = )

Is not equal to ( <> )

Less than ( < )

Greater than ( > )

Less than or equal to ( <= )

Greater than or equal to ( >= )

Multiple conditions

In all of the examples above we have only compared one value, but you can make your conditions much more useful by making use of the following operators:


You can use the and operator to specify that both conditions must be met:


You can use the or operator to specify that either condition must be met: