Did you know that you can return a result from function without using "Result" keyword ?

Did you know that you can return a result from function without using "Result" keyword ?
//------------------------------------
function Add(A, B: Integer): Integer;
begin
  Add := A + B;
end;

Comments

  1. Yes. That's the original way of doing it.

    ReplyDelete
  2. Virgo Pärna So it was the old pascal style .

    ReplyDelete
  3. This "tip" is kind of the wrong way round. The syntax you present here is Pascal. Only with Delphi's proprietary extensions (in this case $X+ extended syntax) are you able to use the special variable Result.

    Now, if only we had a return statement, that would be worth shouting about.

    ReplyDelete
  4. Basics... if you learned pascal in the 80's. Somehow old knowledge get forgotten when new (may be not) better methods are developed... ☺

    Wonder in C++ if anyone Use '?:' something like this:
    (variable>100)? (overhundred=true):(overhundred=false)
    ...


    ReplyDelete
  5. David Heffernan What should "Return" do that cannot be done with "Exit(...)"?

    ReplyDelete
  6. David Heffernan Using Result as variable is only allowed when compiling with $X- .

    ReplyDelete
  7. Mahdi Safsafi No. It's only allowed when compiling $X+

    ReplyDelete
  8. Uwe Raabe Not require you to pass an argument to a function would be a start. The real problem is that the return value is a hidden var parameter passed from caller to callee. It should be a value parameter passed from callee to caller.

    ReplyDelete
  9. Changing that now would probably change code generation, although they could at least make sure that values passed IN are never returned unchanged.

    ReplyDelete
  10. Warren Postma Yeah, I know it's too late to change. It's just galling that they got it so badly wrong.

    ReplyDelete
  11. Are all the system implicit functions (like Inc, Exit etc) implemented as functions? I had thought that it was just special syntax - there would be compiler-generated code in its place.

    ReplyDelete
  12. David Millington if memory serves me right some are just code generation shortcuts.

    ReplyDelete
  13. David Millington Logically they are functions. From the parser's perspective, and from the language's perspective, exit(), inc() etc. are functions or procedures.

    They happen to be implemented as compiler intrinsics.

    ReplyDelete
  14. David Millington You can think as this functions (exit,inc,..) are implemented as inline functions.

    ReplyDelete
  15. It would be nice if the function returns just the last expression like other functional languages.

    ReplyDelete
  16. Khaled Shagrouni Do you mean "return SomeExpression ;" ?

    ReplyDelete
  17. Khaled Shagrouni No, that would be utterly contrary to the design of the language

    ReplyDelete
  18. David Heffernan Are you sure about $X+ ?
    Because i tested the code below and it works without problems:
    //---------------------
    {$X-}

    function Add(A, B: Integer): Integer;
    var
      Result: Integer;
    begin
      Result := A;
      Add := Result + B;
    end;

    However, when i turn to $X+ it does not works !

    ReplyDelete
  19. Mahdi Safsafi Er, remove or rename that local variable. And yes, I am quite sure about $X+.

    ReplyDelete
  20. David Heffernan If declaring Result as variable is allowed only in $X+ mode(as you said before) , how do you explain the fact that my code compiled without problems ?

    ReplyDelete
  21. Mahdi Safsafi I did not say that. Read my original comment again, and note that I do not talk about declaring a variable named Result. I'm not sure that you really understand what Result is in $X+ mode.

    ReplyDelete
  22. Mahdi Safsafi You might have misunderstood David. Declaring "result" as a variable was never the point. Using "result" as the function return value is what the X-directive allows.

    ReplyDelete
  23. Mahdi Safsafi Returning the value of last expression, without using keywords like result , exit, or the name of function:
    function Add(A, B: Integer): Integer;
    begin
       A + B;
    end;

    ReplyDelete
  24. Khaled Shagrouni You don't want this at all. Consider what happens in $X+ mode when you can ignore the value returned by a function. Now you can call a function as though it were a procedure, and have it overwrite Result which is likely not what you want.

    ReplyDelete
  25. David Heffernan I know exactly what does $X means , i just thought that you mean :Result keyword can be used only in $X+ mode .

    ReplyDelete
  26. Khaled Shagrouni I think that is not good for code readability .This will go wrong when having a lot of lines code with many expressions.

    ReplyDelete
  27. Mahdi Safsafi Apparently your understanding of $X+ is incomplete. It controls a number of features. It's impact on function return values is that with $X+ there is a built in special variable named Result, effectively declared by the compiler, that, when you assign to it, is an alias for the function name. This special variable does not exist in $X-.

    ReplyDelete
  28. Mahdi Safsafi This is how most functional programming languages work.

    ReplyDelete
  29. Khaled Shagrouni Delphi is not a functional programming language.

    ReplyDelete
  30. Uwe Raabe something like
    Result := Result + 1;

    ReplyDelete
  31. David Heffernan I think that i have misunderstood you : thought that you mean that declaring result as variable is allowed in $X+ mode.

    ReplyDelete
  32. This feature only applies D2009UP.
    [Qurte]Beginning with Delphi 2009, Exit can take a parameter specifying a result. The parameter must be of the same type as the result of the function. For example:[/Qurte]
    http://docwiki.embarcadero.com/Libraries/XE2/en/System.Exit

    ReplyDelete

Post a Comment