3
votes

In C++ I can write a function like this:

int test(params) { }

And the value returned is only a number (r-value). But it is also possibile to do this:

int& test(params) { }

In this case the function returns a "complete variable". This means that the returned value is not just a value like before but it is a complete variabile having both r-value and l-value.

Is this possibile in Delphi? The first function would be function test(params):integer; but what about the second?

I have seen something similar when I try to implement a parallel for loop. Look at here there is an &. Does it have the reference meaning? I wasnt able to find a good answer by myself.

1
The & is explained in the documentation here: docwiki.embarcadero.com/RADStudio/en/… Essentially it is used to "escape" reserved words - David Heffernan
Thank you I didnt see that in the documentation. I am attending a C++ class and I wanted to do a comparison between delphi and c++ (and not java in this case ofc) so I was confused about the presence of & - Alberto Miola
Yes, finding that in the documentation is tricky. It's a case of you need to know what it means in order to be able to search for it! - David Heffernan

1 Answers

5
votes

Delphi does not support return-by-reference semantics like C++ does. The only options available are either:

  • return a pointer:

    function test(params): PInteger;
    
  • use a var or out output parameter:

    procedure test(params; var output: Integer);
    
    procedure test(params; out output: Integer);