title: Initialise string function result?
body: <p>I've just been debugging a problem with a function that returns a string that has got me worried. I've always assumed that the implicit Result variable for functions that return a string would be empty at the start of the function call, but the following (simplified) code produced an unexpected result:</p>
  
  <pre><code>function TMyObject.GenerateInfo: string;
  
          procedure AppendInfo(const AppendStr: string);
          begin
            if(Result &gt; '') then
              Result := Result + #13;
            Result := Result + AppendStr;
          end;
  
  begin
    if(ACondition) then
      AppendInfo('Some Text');
  end;
  </code></pre>
  
  <p>Calling this function multiple times resulted in:</p>
  
  <pre><code>"Some Text"
  </code></pre>
  
  <p>the first time,</p>
  
  <pre><code>"Some Text"
  "Some Text"
  </code></pre>
  
  <p>the second time,</p>
  
  <pre><code>"Some Text"
  "Some Text"
  "Some Text"
  </code></pre>
  
  <p>the third time, etc.</p>
  
  <p>To fix it I had to initialise the Result:</p>
  
  <pre><code>begin
    Result := '';
    if(ACondition) then
      AppendInfo('Some Text');
  end;
  </code></pre>
  
  <p>Is it necessary to initialise a string function result? Why (technically)? Why does the compiler not emit a warning "W1035 Return value of function 'xxx' might be undefined" for string functions? Do I need to go through all my code to make sure a value is set as it is not reliable to expect an empty string from a function if the result is not explicitly set?</p>
  
  <p>I've tested this in a new test application and the result is the same.</p>
  
  <pre><code>procedure TForm1.Button1Click(Sender: TObject);
  var
    i: integer;
    S: string;
  begin
    for i := 1 to 5 do
      S := GenerateInfo;
    ShowMessage(S); // 5 lines!
  end;
  </code></pre>