473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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.

Oct 28 '07 #1
3 1572
On Sun, 28 Oct 2007 19:25:38 +0100, du*******@gmail.com
<du*******@gmail.comwrote:
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?
The PHP engine is usually smarter then you think, so efficiency wise there
will be very little difference (you can test this offcourse examining time
& 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 the
values in come other scope.
--
Rik Wasmus
Oct 28 '07 #2
..oO(du*******@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
Oct 28 '07 #3
On Oct 28, 2:25 pm, "duzhid...@gmail.com" <duzhid...@gmail.comwrote:
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.

Oct 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
3
by: Miguel | last post by:
Hi, I'm new to .NET and am using VB .NET as I am from a VB background. I am having difficulty understanding the way .NET handles, passes and uses objects. I have had a look at the Micrsoft Data...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
5
by: sfeher | last post by:
Hi All, I need to call a function(loaded with appendChild) for which I have the name as a string. .... var fnName = 'fn1'; var call = fnName + '('+ param +' )'; eval(call);
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
8
by: Peter | last post by:
I will excute the code below to get MethodName array. These codes are edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives me a varning message: The varible "sSourceMethodName"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.