11.4.4 Constant parameters

In addition to variable parameters and value parameters Free Pascal also supports Constant parameters. A constant parameter can be specified as follows:

Constant parameters

                -     ------          -----------------------------------
 constant parameter const  |   identifier list -  -------------           --|   |
                        |               :  -array -of -| type identifier     |
                        -identifier- :-type identifier = -default parameter value|

Specifying a parameter as Constant is giving the compiler a hint that the contents of the parameter will not be changed by the called routine. This allows the compiler to perform optimizations which it could not do otherwise, and also to perform certain checks on the code inside the routine: namely, it can forbid assignments to the parameter. Furthermore a const parameter cannot be passed on to another function that requires a variable parameter: the compiler can check this as well. The main use for this is reducing the stack size, hence improving performance, and still retaining the semantics of passing by value...

Remark: Contrary to Delphi, no assumptions should be made about how const parameters are passed to the underlying routine. In particular, the assumption that parameters with large size are passed by reference is not correct. For this the constref parameter type should be used, which is available as of version 2.5.1 of the compiler.

An exception is the stdcall calling convention: for compatibility with COM standards, large const parameters are passed by reference.

Constant parameters can also be untyped. See section 11.4.2, page 490 for more information about untyped parameters.

As for value parameters, constant parameters can get default values.

Open arrays can be passed as constant parameters. See section 11.4.5, page 495 for more information on using open arrays.