Default Parameters

You can specify default parameter values in a procedure or function heading. Default values are allowed only for typed const and value parameters. To provide a default value, end the parameter declaration with the = symbol followed by a constant expression that is assignment-compatible with the parameter's type.

For example, given the declaration procedure FillArray(A: array of Integer; Value: Integer = 0);

the following procedure calls are equivalent.


FillArray(MyArray, 0);

A multiple-parameter declaration cannot specify a default value. Thus, while function MyFunction(X: Real = 3.5; Y: Real = 3.5): Real; is legal, function MyFunction(X, Y: Real = 3.5): Real; // syntax error is not.

Parameters with default values must occur at the end of the parameter list. That is, all parameters following the first declared default value must also have default values. So the following declaration is illegal.

procedure MyProcedure(I: Integer = 1; S: string); // syntax error

Default values specified in a procedural type override those specified in an actual routine. Thus, given the declarations type TResizer = function(X: Real; Y: Real = 1.0): Real;

the statements

result in the values (n, 1.0) being passed to Resizer.

Default parameters are limited to values that can be specified by a constant expression. Hence parameters of a dynamic-array, procedural, class, class-reference, or interface type can have no value other than nil as their default. Parameters of a record, variant, file, static-array, or object type cannot have default values at all.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment