Delphi Error – W1035 Return value of function ‘%s’ might be undefined

Delphi Compiler Error

W1035 Return value of function ‘%s’ might be undefined

Reason for the Error & Solution

This warning is displayed if the return value of a function has not been assigned a value on every code path.

To put it another way, the function could execute so that it never assigns anything to the return value.

program Produce;
(*$WARNINGS ON*)
var
  B: Boolean;
  C: (Red,Green,Blue);

function Simple: Integer;
begin
end;                         (*<-- Warning here*)

function IfStatement: Integer;
begin
  if B then
    Result := 42;
end;                           (*<-- Warning here*)

function CaseStatement: Integer;
begin
  case C of
  Red..Blue: Result := 42;
  end;
end;                           (*<-- Warning here*)

function TryStatement: Integer;
begin
  try
    Result := 42;
  except
    Writeln('Should not get here!');
  end;
end;                           (*<-- Warning here*)

begin
  B := False;
end.

The problem with procedure IfStatement and CaseStatement is that the result is not assigned in every code path. In TryStatement, the compiler assumes that an exception could happen before Result is assigned.

program Solve;
(*$WARNINGS ON*)
var
  B: Boolean;
  C: (Red,Green,Blue);

function Simple: Integer;
begin
  Result := 42;
end;

function IfStatement: Integer;
begin
  if B then
    Result := 42
  else
    Result := 0;
end;

function CaseStatement: Integer;
begin
  case C of
  Red..Blue: Result := 42;
  else       Result := 0;
  end;
end;

function TryStatement: Integer;
begin
  Result := 0;
  try
    Result := 42;
  except
    Writeln('Should not get here!');
  end;
end;

begin
  B := False;
end.

The solution is to make sure there is an assignment to the result variable in every possible code path.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...