Functions should not leave their result uninitialized, as their return value will then be undefined.
This also includes out parameters, which are designed to populate an uninitialized
variable outside the routine.
For a function, ensure that Result is initialized by the end of every branch:
function GetWeapon(Ranged: Boolean): TWeapon;
begin
if Ranged then begin
Result := TBow.Create;
end;
end;
function GetWeapon(Ranged: Boolean): TWeapon;
begin
if Ranged then begin
Result := TBow.Create;
end
else begin
Result := TSword.Create;
end;
end;
Ensure that all out parameters are initialized by the end of every branch:
procedure GetOutfit(PersonName: string; out Shirt: TShirt; out Pants: TPants);
begin
if (PersonName <> '') then begin
Shirt := GetShirt(PersonName);
Pants := GetPants(PersonName);
end;
end;
procedure GetOutfit(PersonName: string; out Shirt: TShirt; out Pants: TPants);
begin
if (PersonName <> '') then begin
Shirt := GetShirt(PersonName);
Pants := GetPants(PersonName);
end
else begin
Shirt := nil;
Pants := nil;
end;
end;