Methods

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

Functions

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.

Parameters

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.

Exit

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

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.

Loops

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.

Break

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

Continue

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:

Else

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.

Conditions

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:

and

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

 or

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

 

Active Control Copy To Clipboard

A Delphi function that will perform the copy to clipboard function on the active control.

Using ActiveControlCopyToClipboard:

Parameters:

This procedure has no parameters.

Return Value:

This procedure has no return value.

Example:

 

Active Control Cut To Clipboard

A Delphi function that will perform the cut to clipboard function on the active control.

Using ActiveControlCutToClipboard:

Parameters:

This procedure has no parameters.

Return Value:

This procedure has no return value.

Example: