Why does the compiler warn the return value of function 'frog' might be undefined?

This unit generates a warning that the value of frog might be undefined even though result is explicity set:

  unit homePage;

  interface

  function frog(const ts, fs: string; ci: integer; const ss: string; sb, sk, sr, ay, h: integer): string;

  implementation

  function frog(const ts, fs: string; ci: integer; const ss: string; sb, sk, sr, ay, h: integer): string;
  var
    s1, s2, s3, s4, s4a, s5, s6, s7, s8: string;
    m, i, j, n, sc, a, q, si, c, p, ct, f, pg, t: integer;
  begin
  m := 0; i := 0; j := 0; n := 0; sc := 0; a := 0; q := 0; si := 0; c := 0; p := 0; ct := 0; f := 0; pg := 0; t := 0;
  s1 := ''; s2 := ''; s3 := ''; s4 := ''; s4a := ''; s5 := ''; s6 := ''; s7 := ''; s8 := '';
  result := 'x';
  end;

  end.

The compiler (Delphi 6 build 6.240 with update pack 2) rightly warns the values assigned to the integers are never used. The original code (with much more descriptive variable names) actually sets or computes all the variables, so the only warning generated by the original code is about the function's return value; the above code has been minimized and still generates the mysterious warning. Unlike the integers, the compiler does not warn about the string variables never being used.

Just about any change to the above code (deleting or using any of the variables) makes the return value warning go away. No such luck with the original code, which always and only generates the warning the return value might be undefined.

Is this a bug in the compiler?

Upvotes: 2

Views: 197

Answers (2)

David Heffernan
David Heffernan

Reputation: 613432

Is this a bug in the compiler?

Yes. There's really nothing much more to say here. It seems like all those extra local variables confuse the compiler sufficiently for it to spit out this bogus warning.

Upvotes: 2

SilverWarior
SilverWarior

Reputation: 8386

Most common cause for Delphi compiler warning W1035 Return value of function might be undefined is the fact that function result is being only assigned within one of conditional statements like:

Result assigned only within if statement

function MyFunction(AInput: Integer): String;
begin
  if AInput = 5 then
  begin
    Result := 'Some output';
  end;
end;

Result being assigned only within case statement

function MyFunction(AInput: Integer): String;
begin
  case AInput of
    1: Result := 'Some output';
    2: Result := 'Some other output';
  end;
end;

Also since loops are in a way conditional statements where you are repeating same code block multiple times until certain condition is being met you get the same W1035 compiler warning.

Result is being assigned only within for loop

function MyFunction(AInput: Integer): String;
var I: Integer;
begin
  for I = 0 to 10 do
  begin
    Result := 'Some output';
  end;
end;

Result is being assigned only in repeat loop

function MyFunction(AInput: Integer): String;
var I: Integer;
begin
  repeat
    Inc(I,1);
    Result := 'Some output';
  until I = 10;
end;

Result is being assigned only in while loop

function MyFunction(AInput: Integer): String;
var I: Integer;
begin
  while I < 10 do
  begin
    Result := 'Some output';
    Inc(I,1);
  end;
end;

All of these examples will raise W1035 compiler warning. Why? Because there is no guarantee that conditional statements required condition is met to execute that specific part of the code.

PS: If my memory serves me correctly Delphi 6 might also sometimes generate W1035 warning if result was being assigned only within try statement.

So I recommend you check your original code to see if result is being assigned only from within some conditional statements and add some default result assignment on the start of the very function.

Upvotes: 0

Related Questions