Show: Delphi C++
Display Preferences

W1015 FOR-Loop variable '%s' cannot be passed as var parameter (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi)

An attempt has been made to pass the control variable of a FOR-loop to a procedure or function which takes a var parameter. This is a warning because the procedure which receives the control variable is able to modify it, thereby changing the semantics of the FOR-loop which issued the call.


program Produce;

  procedure p1(var x : Integer);
  begin
  end;

  procedure p0;
    var
      i : Integer;
  begin
    for i := 0 to 1000 do
      p1(i);
  end;

begin
end.

In this example, the loop control variable, i, is passed to a procedure which receives a var parameter. This is the main cause of the warning.


program Solve;
  procedure p1(x : Integer);
  begin
  end;

  procedure p0;
    var
      i : Integer;
  begin
    i := 0;
    while i <= 1000 do
      p1(i);
  end;


begin
end.

The easiest way to approach this problem is to change the parameter into a by-value parameter. However, there may be a good reason that it was a by-reference parameter in the begging, so you must be sure that this change of semantics in your program does not affect other code. Another way to approach this problem is change the for loop into an equivalent while loop, as is done in the above program.

Personal tools
In other languages
Previous Versions
Assistance