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;
//------------------------------------
function Add(A, B: Integer): Integer;
begin
Add := A + B;
end;
Yes. That's the original way of doing it.
ReplyDeleteVirgo Pärna So it was the old pascal style .
ReplyDeleteThis "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.
ReplyDeleteNow, if only we had a return statement, that would be worth shouting about.
Basics... if you learned pascal in the 80's. Somehow old knowledge get forgotten when new (may be not) better methods are developed... ☺
ReplyDeleteWonder in C++ if anyone Use '?:' something like this:
(variable>100)? (overhundred=true):(overhundred=false)
...
David Heffernan What should "Return" do that cannot be done with "Exit(...)"?
ReplyDeleteDavid Heffernan Using Result as variable is only allowed when compiling with $X- .
ReplyDeleteMahdi Safsafi No. It's only allowed when compiling $X+
ReplyDeleteUwe 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.
ReplyDeleteChanging that now would probably change code generation, although they could at least make sure that values passed IN are never returned unchanged.
ReplyDeleteWarren Postma Yeah, I know it's too late to change. It's just galling that they got it so badly wrong.
ReplyDeleteAre 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.
ReplyDeleteDavid Millington if memory serves me right some are just code generation shortcuts.
ReplyDeleteDavid Millington Logically they are functions. From the parser's perspective, and from the language's perspective, exit(), inc() etc. are functions or procedures.
ReplyDeleteThey happen to be implemented as compiler intrinsics.
David Millington You can think as this functions (exit,inc,..) are implemented as inline functions.
ReplyDeleteIt would be nice if the function returns just the last expression like other functional languages.
ReplyDeleteKhaled Shagrouni Do you mean "return SomeExpression ;" ?
ReplyDeleteKhaled Shagrouni No, that would be utterly contrary to the design of the language
ReplyDeleteDavid Heffernan Are you sure about $X+ ?
ReplyDeleteBecause 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 !
Mahdi Safsafi Er, remove or rename that local variable. And yes, I am quite sure about $X+.
ReplyDeleteDavid 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 ?
ReplyDeleteMahdi 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.
ReplyDeleteMahdi 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.
ReplyDeleteMahdi Safsafi Returning the value of last expression, without using keywords like result , exit, or the name of function:
ReplyDeletefunction Add(A, B: Integer): Integer;
begin
A + B;
end;
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.
ReplyDeleteDavid Heffernan I know exactly what does $X means , i just thought that you mean :Result keyword can be used only in $X+ mode .
ReplyDeleteKhaled Shagrouni I think that is not good for code readability .This will go wrong when having a lot of lines code with many expressions.
ReplyDeleteMahdi 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-.
ReplyDeleteMahdi Safsafi This is how most functional programming languages work.
ReplyDeleteKhaled Shagrouni Delphi is not a functional programming language.
ReplyDeleteUwe Raabe something like
ReplyDeleteResult := Result + 1;
David Heffernan I think that i have misunderstood you : thought that you mean that declaring result as variable is allowed in $X+ mode.
ReplyDeleteThis feature only applies D2009UP.
ReplyDelete[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