next up previous contents index
Next: Function overloading Up: Using functions and procedures Previous: Function declaration

Parameter lists

 

When you need to pass arguments to a function or procedure, these parameters must be declared in the formal parameter list of that function or procedure.

The parameter list is a declaration of identifiers that can be referred to only in that procedure or function's block.


Parameters

syntdiag4078

syntdiag4082

const parameters and var parameters can also be untyped parameters if they have no type identifier.

Value parameters

Value parameters are declared as follows:


Value parameters

syntdiag4110

When you declare parameters as value parameters, the procedure gets a copy of the parameters that the calling block passes. Any modifications to these parameters are purely local to the procedure's block, and do not propagate back to the calling block.

A block that wishes to call a procedure with value parameters must pass assignment compatible parameters to the procedure. This means that the types should not match exactly, but can be converted (conversion code is inserted by the compiler itself)

Take care that using value parameters makes heavy use of the stack, especially if you pass large parameters. The total size of all parameters in the formal parameter list can only be 64K (on an Intel machine).

You can pass open arrays as value parameters. See section (gif) for more information on using open arrays.

var parameters

 

Variable parameters are declared as follows:


Variable parameters

syntdiag4149

When you declare parameters as variable parameters, the procedure or function accesses immediatly the variable that the calling block passed in its parameter list. The procedure gets a pointer to the variable that was passed, and uses this pointer to access the variable's value. From this, it follows that any changes that you make to the parameter, will proagate back to the calling block. This mechanism can be used to pass values back in procedures.

Because of this, the calling block must pass a parameter of exactly the same type as the declared parameter's type. If it does not, the compiler will generate an error.

Variable parameters can be untyped. In that case the variable has no type, and hence is incompatible with all othertypes. However, you can use the address operator on it, or you can pass it to a function that has also an untyped parameter. If you want to use an untyped parameter in an assigment, or you want to assign to it, you must use a typecast.

File type variables must always be passed as variable parameters.

You can pass open arrays as variable parameters. See section (gif) for more information on using open arrays.

Const parameters

In addition to variable parameters and value parameters Free Pascal also supports Const parameters. You can specify a Const parameter as follows:


Constant parameters

syntdiag4223

A constant argument is passed by reference if it's size is larger than a longint. It is passed by value if the size equals 4 or less. This means that the function or procedure receives a pointer to the passed argument, but you are not allowed to assign to it, this will result in a compiler error. Likewise, you cannot pass a const parameter on to another function that requires a variable parameter.

The main use for this is reducing the stack size, hence improving performance, and still retaining the semantics of passing by value...

Constant parameters can also be untyped. See section (gif) for more information about untyped parameters.

You can pass open arrays as constant parameters. See section (gif) for more information on using open arrays.

Open array parameters

 

Free Pascal supports the passing of open arrays, i.e. you can declare a procedure with an array of unspecified length as a parameter, as in Delphi.

Open array parameters can be accessed in the procedure or function as an array that is declared with starting starting index 0, and last element index High(paremeter).

For example, the parameter
listing4175
would be equivalent to
listing4177
Where N would be the actual size of the array that is passed to the function. N-1 can be calculated as High(Row).

Open parameters can be passed by value, by reference or as a constant parameter. In the latter cases the procedure receives a pointer to the actual array. In the former case,it receives a copy of the array.

In a function or procedure, you can pass open arrays only to functions which are also declared with open arrays as parameters, not to functions or procedures which accept arrays of fixed length.

The following is an example of a function using an open array:


listing4183


next up previous contents index
Next: Function overloading Up: Using functions and procedures Previous: Function declaration

Michael Van Canneyt
Fri Sep 25 09:15:40 MEST 1998