Show: Object Pascal C++
Display Preferences

W1035 Return value of function '%s' might be undefined (Object Pascal)

From Appmethod Topics
Jump to: navigation, search

Go Up to Error and Warning Messages (Object Pascal) Index

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.

Personal tools
In other languages