Sign in Join
Wiki copytemporary

Revision 1 of 2, by WikiArchive on 11-4-2026. The page as it is now.

Basic Pascal Tutorial/Chapter 4/Functions

4C - Functions (author: Tao Yue, state: unchanged)

Functions work the same way as procedures, but they always return a single value to the main program through its own name:

pascal
function Name (parameter_list) : return_type;

Functions are called in the main program by using them in expressions:

pascal
a := Name (5) + 3;

If your function has no argument, be careful not to use the name of the function on the right side of any equation inside the function. That is:

pascal
function Name : integer;
begin
  Name := 2;
  Name := Name + 1
end.

is a no-no. Instead of returning the value 3, as might be expected, this sets up an infinite recursive loop in certain language modes (e.g. {$MODE DELPHI} or {$MODE TP}; other modes require brackets for function call even if the brackets are empty due to no parameters being required for the particular function). Name will call Name, which will call Name, which will call Name, etc.

The return value is set by assigning a value to the function identifier.

pascal
Name := 5;

It is generally bad programming form to make use of VAR parameters in functions -- functions should return only one value. You certainly don't want the sin function to change your pi radians to 0 radians because they're equivalent -- you just want the answer 0.


From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 4/Functions", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.