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.