Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-10

Dynamic Arrays in Procedures

6 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I'm wondering if there is a way to pass an array of values to a Procedure without defining an array variable..

For example, if I have a Procedure:

Procedure DoSomething(X :Byte);

Then I can call it with a specific a variable like this:

Var
   ThingA :Byte;
.
DoSomething(ThingA);

And I can also call it without a variable but instead just hard code a specific value like this:

DoSomething($1A);

So if I have a procedure like this:

Procedure DoSomethingElse(Y :Array of Byte);
I can call it like this:
Var
   ThingB :Array Of Byte;
SetLength(ThingB,3);
ThingB[0] :=$12;
ThingB[1] :=$1A;
ThingB[2] :=$2B;
DoSomethingElse(ThingB);

But can I also just call it with specific values somehow? DoSomethingElse([$12,$1A,$2B]); for example. of course this doesn't work, but is there a syntax that would work?

James

Reply by email

Howard Page-Clark
to James Richters
James Richters wrote 49 lines
I’m wondering if there is a way to pass an array of values to a Procedure without defining an array variable…. For example, if I have a Procedure: Procedure DoSomething(X :Byte); Then I can call it with a specific a variable like this: Var ThingA :Byte; … DoSomething(ThingA); And I can also call it without a variable but instead just hard code a specific value like this: DoSomething($1A); So if I have a procedure like this: Procedure DoSomethingElse(Y :Array of Byte); I can call it like this: Var ThingB :Array Of Byte; SetLength(ThingB,3); ThingB[0] :=$12; ThingB[1] :=$1A; ThingB[2] :=$2B; DoSomethingElse(ThingB); But can I also just call it with specific values somehow? DoSomethingElse([$12,$1A,$2B]);for example… of course this doesn’t work, but is there a syntax that would work?

There is array of const syntax.

== code==

program project1;
{$mode objfpc}
procedure ShowArray(anArray: array of const);
var
   i: Integer;
begin
   for i := Low(anArray) to High(anArray) do
     case anArray[i].VType of
       vtInteger: WriteLn(anArray[i].VInteger,' ');
       vtChar: WriteLn(anArray[i].VChar,' ');
     end;
end;
begin
   ShowArray([99, -1, 'p', 'G']);
   ReadLn;
end.

== code end==

Reply by email

James Richters
to gabor

So it does...

I should have tested my own example. I found my problem, I declared it as Var and it would only work with variables due to that.

Earlier message 12 lines
-----Original Message----- From: fpc-pascal On Behalf Of gabor via fpc-pascal Sent: Sunday, October 4, 2020 4:34 PM To: Cc: gabor Subject: Re: [fpc-pascal] Dynamic Arrays in Procedures W dniu 2020-10-04 o 21:58, James Richters via fpc-pascal pisze: > DoSomethingElse([$12,$1A,$2B]);for example… of course this doesn’t > work, but is there a syntax that would work? This expression works fine with FPC 3.2.0 _______________________________________________

Reply by email

Winfried Bartnick
to James Richters
James Richters wrote 17 lines
So it does... I should have tested my own example. I found my problem, I declared it as Var and it would only work with variables due to that. -----Original Message----- From: fpc-pascal On Behalf Of gabor via fpc-pascal Sent: Sunday, October 4, 2020 4:34 PM To: Cc: gabor Subject: Re: [fpc-pascal] Dynamic Arrays in Procedures W dniu 2020-10-04 o 21:58, James Richters via fpc-pascal pisze: > DoSomethingElse([$12,$1A,$2B]);for example… of course this doesn’t > work, but is there a syntax that would work? This expression works fine with FPC 3.2.0 _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Hi!

Works also  fine with fpc  3.04

Winni

Reply by email

Sven Barth
to James Richters: So it does... I should have tested my own example. I found my problem, I declared it as…

James Richters via fpc-pascal schrieb am
So., 4. Okt. 2020, 23:44:

Show quoted text 5 lines
So it does... I should have tested my own example. I found my problem, I declared it as Var and it would only work with variables due to that

That's just a normal open array parameter and worked this way for a long time already.

What's new in 3.2.0 is that you can use dynamic array parameters together with array constructors as well.

Important: if you have "array of xxx" directly inside a parameter clause that ja *always* an open array. For a dynamic array you need to explicitly declare an array type or use an inline specialization of TArray.

Regards,
Sven

Reply by email