Passing by reference better than returning a value when calling a function?

Passing by reference better than returning a value when calling a function?

am 28.10.2007 19:25:38 von duzhidian

Hello:

When I need a function to generate a string, I have two methods:

1.
pass by reference:

mystring = "";
myfunction( mystring ); // prototype of myfunction( &anystring );

OR 2.
mystring = myfunction(); //which return a string

According to experience of C/C++, the former one is better as of the
efficient, how about PHP?

Thanks.

Re: Passing by reference better than returning a value when calling a function?

am 28.10.2007 20:04:15 von luiheidsgoeroe

On Sun, 28 Oct 2007 19:25:38 +0100, duzhidian@gmail.com =

wrote:

> Hello:
>
> When I need a function to generate a string, I have two methods:
>
> 1.
> pass by reference:
>
> mystring =3D "";
> myfunction( mystring ); // prototype of myfunction( &anystring );
>
> OR 2.
> mystring =3D myfunction(); //which return a string
>
> According to experience of C/C++, the former one is better as of the
> efficient, how about PHP?

The PHP engine is usually smarter then you think, so efficiency wise the=
re =

will be very little difference (you can test this offcourse examining ti=
me =

& memory usage). From a logical standpoint though, it makes very little =
=

sense to create a _new_ string in an existing variable by reference. I'd=
=

say functions taking arguments by reference only make sense when they =

either alter data directly related to their 'previous' value, or store t=
he =

values in come other scope.
-- =

Rik Wasmus

Re: Passing by reference better than returning a value when calling a function?

am 28.10.2007 22:39:34 von Michael Fesser

..oO(duzhidian@gmail.com)

>When I need a function to generate a string, I have two methods:
>
>1.
>pass by reference:
>
>mystring = "";
>myfunction( mystring ); // prototype of myfunction( &anystring );
>
>OR 2.
>mystring = myfunction(); //which return a string
>
>According to experience of C/C++, the former one is better as of the
>efficient, how about PHP?

PHP is not C/C++. Don't compare C's references or pointers with
references in PHP.

If you really have a use for references, then use them. But in all other
cases just use what makes more sense in the application context. Usually
I prefer to have a function return its result, since this allows to
easily nest function calls. The code is more readable (at least for me).

Actually I think that in my own framework there's currently not a single
function that requires arguments passed by reference. Some functions use
references internally, for example for traversing through an array tree,
but in most if not all functions arguments are passed by value.

Micha

Re: Passing by reference better than returning a value when calling a function?

am 28.10.2007 23:49:18 von zeldorblat

On Oct 28, 2:25 pm, "duzhid...@gmail.com" wrote:
> Hello:
>
> When I need a function to generate a string, I have two methods:
>
> 1.
> pass by reference:
>
> mystring = "";
> myfunction( mystring ); // prototype of myfunction( &anystring );
>
> OR 2.
> mystring = myfunction(); //which return a string
>
> According to experience of C/C++, the former one is better as of the
> efficient, how about PHP?
>
> Thanks.

In addition to what everyone else said (and they're both absolutely
correct) I tried both ways through a profiler. Assigning the return
value was about 40% faster than passing the reference.

So, to reiterate, do what makes sense, not what you /think/ will be
faster. If you run into performance problems later then you can
systematically optimize using a profiler or similar tool.

Google for "premature optimization" for more on the subject.