4

Example

procedure TForm1.ButtonClick(Sender: TObject);
var x:integer;
begin
   SetLength(MyArray,10)
   for x:=0 to 9 do FillWithRandomNumbers(MyArray[x]);
end;

Procedure FillWithRandomNumbers(var MyArray: Array of double);
begin
  MyArray:=Random; //<-I have no idea what to do here :(
end;

As you can see I'm trying to pass single element to procedure in order to perform some task on specified array cell. For example procedure FillWithRandomNumbers should take MyArray[2] and fill this cell with random number.

1 Answer 1

4

You want to pass a single array element, yet your procedure expects a full array. To directly answer your actual question, your procedure should be defined as:

Procedure FillWithRandomNumber(var Value: double);  
begin
  Value:= Random;
end;

procedure TForm1.ButtonClick(Sender: TObject);
var x:integer;
begin
   SetLength(MyArray,10)
   for x:=0 to 9 do FillWithRandomNumber(MyArray[x]);
end;

Or you could do it like this instead:

procedure TForm1.ButtonClick(Sender: TObject);
begin
   SetLength(MyArray, 10);
   FillWithRandomNumbers(MyArray);
end;

Procedure FillWithRandomNumbers(var SomeArray: Array of double);
var
  X: Integer;
begin
  for X := Low(SomeArray) to High(SomeArray) do begin
    SomeArray[X] := Random;
  end;
end;

Or to be even more simple, just don't use a procedure at all:

procedure TForm1.ButtonClick(Sender: TObject);
var
  X: Integer;
begin
  SetLength(MyArray, 10);
  for X := 0 to High(Array) do begin
    MyArray[X]:= Random;
  end;
end;
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I know It can be done simpler. It was just an extreme simple example. Thank you for first solution.
Another possibility is to use the result of a funciton: function GetRandomNumber; begin Result:=Random; end; procedure TForm1.ButtonClick(Sender: TObject); var x:integer; begin SetLength(MyArray,10) for x:=0 to 9 do MyArray[x] := GetRandomNumber(); end;

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.