Real world examples of pass by reference

followup to question:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28229102.html#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
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Why would you fry an egg?  Because you wanted to have a fried egg.  It's just something that you can do, not something that always makes sense.

A real world example would be one that involved a very large in-memory structure like a huge array.  If the in-memory structure was so big that duplicating it would risk overrunning your memory allocation, you might program your way around it by using the & notation to cause the functions to operate on the original array, instead of making a copy of the array (thus using twice as much memory).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
Relating this to windows7

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

maybe just drag and drop the folder
0
Ray PaseurCommented:
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.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

rgb192Author Commented:
thank you about the memory explanation.
0
Ray PaseurCommented:
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
0
rgb192Author Commented:
Eventually I will become a programmer that has an objective to use the '&'
0
Ray PaseurCommented:
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
0
gr8gonzoConsultantCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.