Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

Real world examples of pass by reference

followup to question:
https://www.experts-exchange.com/questions/28229102/copy-by-identifier-versus-copy-by-reference.html?anchorAnswerId=39460164#a39460164

Could you please show me real world explanations of pass by reference.

Why would a website use this.


$y = addone(&$x);
All I understand is changing both $x and $y in the same statement but I still do not understand why
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

Relating this to windows7

Instead of cut and paste a folder
(or copy paste, then delete)

maybe just drag and drop the folder
That's not a bad analogy at all!  Copy and paste would take up twice as much memory, whereas drag-and-drop would use the original data without making a copy.
Avatar of rgb192

ASKER

thank you about the memory explanation.
While that's a good enough answer, there may be other situations and concepts that make sense.  Sometimes it's just about the programmers' objectives.  I don't have all the answers!  But thanks for the points anyway, ~Ray
Avatar of rgb192

ASKER

Eventually I will become a programmer that has an objective to use the '&'
Naw, don't do that just yet.  Become a programmer who writes the simplest, most elegant, easy-to-understand code.  Become one who teaches with every example.  Become one who has good comments and shows clarity of vision.  Do all of those things first, then use the & for something.

You're a good student.  I'm glad you're using EE! ~Ray
Avatar of gr8gonzo
This has already been answered, but I'm adding a few thoughts:

1. You shouldn't ever pass a parameter into a function by its call:
$y = addone(&$x);

Instead, your FUNCTION should be the one that designates what is passed by parameter. Notice the & is in the function definition, not in the call itself:

$y = addone($x);
function addone(&$parameter)
{
   ....
}

If you try to do it the first way, PHP will throw a warning about deprecated behavior. This is because PHP needs to be able to "prepare" for what is about to happen. Otherwise, you could write this code:

$a = addone(&$b);
$y = addone($x);

Now the same function is being called in two different ways, so PHP has to work harder to accommodate both types of calls. That is why it is deprecated. The second approach I listed will properly handle passing parameters by reference.

2. Objects are always passed by reference. So if you have:
$x = new House();
$y = addone($x);
function addone($parameter)
{
   ...
}

...then $parameter would be a reference to $x.

3. The two most common uses of passing-by-reference are almost always:

a. Memory efficiency: If you have an array of data that is taking up 100 megs of memory, then the last thing you want to do is pass it by value and have it take up another 100 megs of memory.

b. Recursive functions that are modifying a value: This is a little hard to explain without a good example, but a recent example for me was a function that recursively went through an XML structure and removed unnecessary bits of data in order to compress the structure. If I hadn't passed in the values by reference, then I'd be stuck with trying to return arrays and objects and merging them back into the original structure and it would've been a mess. Instead, the function simply modifies the value and the changes are made right then and there.