Alejandria:
Me gustaría hacer algo como esto:
procedure show(a : Array of Integer);
var
i : integer;
begin
for i in a do
writeln(i);
end;
begin
show((1, 2));
show((3, 2, 5));
end.
pero esto es lo más cerca que llegué
Program arrayParameter(output);
type
TMyArray = Array[0..2] of Integer;
var
arr : TMyArray = (1, 2, 3);
procedure show(a : TMyArray);
var
i : integer;
begin
for i in a do
writeln(i);
end;
begin
show(arr);
end.
Así que tengo que declarar una matriz diferente para cada vez que quiera llamar a la función? Sírvanse proporcionar un ejemplo de trabajo.
Andreas Rejbrand:
Si lo haces
procedure show(a: array of Integer);
var
i: Integer;
begin
for i in a do
Writeln(i);
end;
entonces usted puede escribir
show([1, 2, 3, 4]);
Este tipo de parámetro de matriz se llama un parámetro de matriz abierta . Si una función tiene un parámetro de matriz abierta, puede darle a la vez dinámico y arreglos estáticos, además de estos "conjuntos de literales". Por lo tanto, dado nuestro show
procedimiento, también podemos hacer
var
DynArr: TArray<Integer>; // = array of Integer
StaticArr: array[0..2] of Integer;
begin
show(DynArr);
show(StaticArr);
end;
Sólo por comparación: Si en lugar de hacerlo
procedure show(a: TArray<Integer>);
o tiene una
type
TDynIntArray = array of Integer;
y hacer
procedure show(a: TDynIntArray);
a continuación, show
sólo aceptará dichas matrices dinámicas.