Initialise string function result?

前端 未结 7 2081
粉色の甜心
粉色の甜心 2020-12-02 18:33

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

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 19:06

    Alex's answer is nearly always right and it answers why I was seeing the strange behaviour that I was, but it isn't the whole story.

    The following, compiled without optimisation, produces the expected result of sTemp being an empty string. If you swap the function out for the procedure call you get a different result.

    There seems to be a different rule for the actual program unit.

    Admittedly this is a corner case.

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses System.SysUtils;
    
      function PointlessFunction: string;
      begin
      end;
    
      procedure PointlessProcedure(var AString: string);
      begin
      end;
    
    var
      sTemp: string;
    begin
      sTemp := '1234';
      sTemp := PointlessFunction;
      //PointlessProcedure(sTemp);
      WriteLn('Result:' + sTemp);
      ReadLn;
    end.
    

提交回复
热议问题