3

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.

4
  • The & is explained in the documentation here: docwiki.embarcadero.com/RADStudio/en/… Essentially it is used to "escape" reserved words Commented May 19, 2017 at 7:56
  • See also: Is the practice of returning a C++ reference variable, evil? Commented May 19, 2017 at 7:57
  • 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 & Commented May 19, 2017 at 8:01
  • 1
    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! Commented May 19, 2017 at 8:08

1 Answer 1

5

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);
    
Sign up to request clarification or add additional context in comments.

7 Comments

But if you want to return a full value, please do not return pointers! The var/out solution is the best,
@RudyVelthuis That blanket statement doesn't make much sense to me. Sometimes returning a reference is appropriate, sometimes it is not. It depends on what you are trying a achieve.
@Remy var or out do not correspond to returning a reference. They correspond to passing in references.
@David: If a value or record must be returned, pointers are always a bad choice. Yes, it is a blanket statement, and if the pointer is just a pointer into to something that was passed in, it's fine, but generally, that is not the case. Generally, it is bad habit to return values through pointers.
@Rudy You don't understand. If you want to return a reference, return a reference. If you want to return a value, return a value. The type is irrelevant. Sometimes you want to return a reference, sometimes a value. You can't make a blanket statement. If I want to allow the caller to modify some value that I own, returning a value doesn't get it done. And vice versa.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.