473,396 Members | 1,859 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,396 software developers and data experts.

Passing arguments by reference to functions



Having re-read www.php.net/functions.arguments recently, the notion of
passing arguments by reference to a function makes a lot more sense now.

However, my question is: is there any difference in outcome between:

function add_some_extra(&$string) {
$string .= 'and something extra.';
}

add_some_extra($str);

and
function add_some_extra($string) {
$string .= 'and something extra.';
return $string;
}

$str = add_some_extra($str);
other than presumably that the former is less memory-intensive?
Martin

Jul 17 '05 #1
4 6346
Martin Lucas-Smith wrote:

Having re-read www.php.net/functions.arguments recently, the notion of
passing arguments by reference to a function makes a lot more sense now.

However, my question is: is there any difference in outcome between:

function add_some_extra(&$string) {
$string .= 'and something extra.';
}

add_some_extra($str);

and
function add_some_extra($string) {
$string .= 'and something extra.';
return $string;
}

$str = add_some_extra($str);


Hi,

there is no difference really i would say.
Calling by Reference also doesn't make much
sense here.
But imagine you have an object you would like
to manipulate inside another object.
There you would pass the other object by reference.
For instance if you have an object car and inside you
want to create 4 objects of the type wheel.
Then i would make the wheels and save them
as References.
Otherwise you would lose them
after you leave the car constructor...
ehmmm... jeez... :) that sounds confusing...? :)

Sandy

Jul 17 '05 #2
Martin Lucas-Smith wrote:
other than presumably that the former is less memory-intensive?


It is not. It has something to do with the extra smart
copy-on-write-reference-counting mechanism that the Zend engine uses.
You can read it up in "Core PHP", which also has a full explanation of
the reasons under the heading "Don't Trust Your Instincts".

But much worse is IMHO the fact that you can't use literals as
arguments, like somefunction(array('a','b')).

Jochen

Jul 17 '05 #3
it just depend on what kind of treatement you want to do on the object passed to the function.
If you want to treat the same object than the original then you use reference, if you are about treating a copy, then you dont use
reference. If you pass a car to the function using reference then if inside the function you change the wheels, then when you exit
the function, the original car will also have the wheels changed.

Savut

"Jochen Buennagel" <za**@buennagel.com> a écrit dans le message de news:br*************@news.t-online.com...
Martin Lucas-Smith wrote:
other than presumably that the former is less memory-intensive?


It is not. It has something to do with the extra smart
copy-on-write-reference-counting mechanism that the Zend engine uses.
You can read it up in "Core PHP", which also has a full explanation of
the reasons under the heading "Don't Trust Your Instincts".

But much worse is IMHO the fact that you can't use literals as
arguments, like somefunction(array('a','b')).

Jochen

Jul 17 '05 #4
Martin Lucas-Smith wrote:

Having re-read www.php.net/functions.arguments recently, the notion of
passing arguments by reference to a function makes a lot more sense now.

However, my question is: is there any difference in outcome between:
function add_some_extra(&$string) {
$string .= 'and something extra.';
}

add_some_extra($str);

and
function add_some_extra($string) {
$string .= 'and something extra.';
return $string;
}

$str = add_some_extra($str);

There's at least three *major* diffs : the second version
- does not have side-effect, which means you *don't* modify the argument
(and this is a Good Thing(tm)).

- return something, which means you can do thangs like :
$mystr ="here is a string ";
print add_some_extra($mystr)

- can be used with litterals, ie :
print add_some_extra("here is another string");

So the second version is IMHO *much* more clean and useful.
other than presumably that the former is less memory-intensive?


Here you maybe wrong... (see other posts in this thread).

Bruno

Jul 17 '05 #5

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) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
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...
32
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
4
by: Bit Byte | last post by:
Noobie here (C++/C/Java experience though) ... Recently picked up PHP ... I want to pass an array to a function and then to use count on the passed variable - is this the way to do it (its an...
1
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. ...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
3
by: Subodh | last post by:
Hi All, In C++ we could pass a constant reference to a function so that the target function could not modify the objects passed eg. const classA & dummmyfunction(const classB) similar thing...
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:
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.