1

Is it possible to pass to a function or procedure a dynamic array as optional parameter ? If yes, how ?

I have tried in this way :

procedure testp (str : string; var arr : StringArray = nil);
begin
    str := 'Ciao Alessio !';
    SetLength(arr, 2);
    arr[0] := 'Ale';
    arr[1] := 'Ale';
end;

but it gives : default parameter 'arr' must be by-value or const .

I'm using Delphi 7, but if not possible with Delphi 7, is it possible with newer version of Delphi or Free Pascal ?

3
  • It's poor form to use var parameters like this; it's not clear in the code that the parameter will be modified. It would be better to use a function to return the value. Commented Oct 10, 2013 at 20:42
  • Why not have a TStrings parameter and pass in nil? Arrays of strings SUCK, if you want a fast and friendly API, just use TStringList (base class TStrings) in your parameters. Why write a million find-a-string-in-array routines, when you could just be using a class instead? Commented Oct 10, 2013 at 22:59
  • 1
    Because with arrays (I mean openArrays) you could simply use array constructor [] just when you pass parameters in procedure call, but with TStrings you have to declare var, create, destroy, all that delphi shit. Commented Feb 28, 2017 at 8:28

2 Answers 2

5

Default parameters can only be specified for const or by value parameters. They cannot be specified for var parameters.

To achieve the caller flexibility you are looking for you'll need to use overloads.

procedure foo(var arr: StringArray); overload;
begin
  .... do stuff
end;

procedure foo; overload;
var
  arr: StringArray;
begin
  foo(arr);
end;
Sign up to request clarification or add additional context in comments.

2 Comments

So, there is no way to initialize the dynamic array empty as default parameter. Overloading is the only way ?
Yes that is so. A var parameter passes a reference to a variable. And if you use a default parameter, there is no variable to take a reference of.
1

The error message means exactly what it says, and it has nothing to do with the parameter being a dynamic array. The compiler would have rejected that code no matter what type the parameter had because you're not allowed to give default values for parameters passed by reference.

To make an optional reference parameter, use overloading to give two versions of the function. Change your current function to receive its parameter by value or const, as the compiler advises, and then declare another function without that parameter, as follows:

procedure testp (str : string);
var
  arr: StringArray;
begin
  testp(str, arr);
end;

That is, declare a dummy parameter and pass it to the "real" function. Then simply throw away the value it returns.

If calculation of the reference value is expensive, then the implementation of the one-parameter version of testp will instead duplicate more of the code from the two-argument version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.