In Delphi, are parameters evaluated in order when passed into a method?

前端 未结 2 1102
南旧
南旧 2020-12-17 15:38

Is the order in which parameters are calculated before a procedure is called defined in Delphi?

IOW, if I have this ugly code (found something like this in a legacy

相关标签:
2条回答
  • 2020-12-17 16:10

    Edited: it seems that the compiler may violate the behavior described in the help:

    From Calling Conventions help topic (emphasis mine):

    The register and pascal conventions pass parameters from left to right; that is, the left most parameter is evaluated and passed first and the rightmost parameter is evaluated and passed last.

    0 讨论(0)
  • 2020-12-17 16:17

    The order of parameter evaluation in Delphi is not defined.

    As an interesting demonstration of this, the following program has different output depending on whether you target 32 or 64 bit code:

    program ParameterEvaluationOrder;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils;
    
    function SideEffect(A: Integer): Integer;
    begin
      Writeln(A);
      Result := A;
    end;
    
    procedure Test(A, B: Integer);
    begin
    end;
    
    begin
      Test(SideEffect(1), SideEffect(2));
      Readln;
    end.
    
    0 讨论(0)
提交回复
热议问题