473,508 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which is better, pass, or not pass by reference?


Folks,

I've asked this before but never got any response but its important and I
thought I'd pitch it again (hopefully a bit clearer)...

One can pass data from one function to another either by passing a copy, or
passing by reference.

My understanding of passing by reference (putting the & before a variable
during the function declaration) means that the original data is used.

My understanding of *not* passing by reference, (thus passing a copy)
literally
means creating a copy of the data contained in my variables - thus doubling
the memory usage for this data... even if its just a temporary while the
function is being executed.

I *believed* (past tense) that one should only pass by reference if they
wanted the child function to change the data and pass the newer values back
to the parent function.

I'm now thinking that this is just a feature and not the only usage. I am
begining to think I should use "pass by reference" nearly all the time in
order to conserve memory (since I won't be creating copies of my variable
data in memory).

Would this be good practice? Am I right that passing by reference means that
my variable data is not duplicated thus using less memory than if I had
passed my variable data as a copy?
--
All comments + replies please via the newsgroup,
Thanks,
Randell D.
-
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?
Jul 17 '05 #1
6 9252

"Randell D." <yo**************@randelld.at.yahoo.com> schreef in bericht
news:cU4lb.138072$6C4.80562@pd7tw1no...

Folks,

I've asked this before but never got any response but its important and I
thought I'd pitch it again (hopefully a bit clearer)...

One can pass data from one function to another either by passing a copy, or passing by reference.

My understanding of passing by reference (putting the & before a variable
during the function declaration) means that the original data is used.

My understanding of *not* passing by reference, (thus passing a copy)
literally
means creating a copy of the data contained in my variables - thus doubling the memory usage for this data... even if its just a temporary while the
function is being executed.

I *believed* (past tense) that one should only pass by reference if they
wanted the child function to change the data and pass the newer values back to the parent function.

I'm now thinking that this is just a feature and not the only usage. I am
begining to think I should use "pass by reference" nearly all the time in
order to conserve memory (since I won't be creating copies of my variable
data in memory).

Would this be good practice? Am I right that passing by reference means that my variable data is not duplicated thus using less memory than if I had
passed my variable data as a copy?
--
All comments + replies please via the newsgroup,
Thanks,
Randell D.

Passing by reference is usefull if you want a function to return more than
only one value... a function returns normally only one value (although you
can return an array with multiple results too)...

Using a reference is done if you want to change a var outside the scope of
your function....

Yes, interesting, why not always use call by reference??? Only thing i can
think is that you do not always want to change values outside the
function...

Regards,

Marcel
Jul 17 '05 #2
"Randell D." <yo**************@randelld.at.yahoo.com> wrote in message
news:cU4lb.138072$6C4.80562@pd7tw1no...
I'm now thinking that this is just a feature and not the only usage. I am
begining to think I should use "pass by reference" nearly all the time in
order to conserve memory (since I won't be creating copies of my variable
data in memory).


_if_ I am correct, php 5 will pass by reference by default.

rush
--
http://www.templatetamer.com/

Jul 17 '05 #3
Reference is useful only for object.

if your code is object oriented, then you should always return
references to your objects, never copies, or you will have some
surprises (change the object in one place, without changing it somewhere
else is bad)

for built-in types like strings, integers, booleans , return copies, you
will double the use of memory but for this kind of elements, it has
absolutly NO importance (I'm not even sure you can reference a string
for example).

Now even if your code is not OO, you still have some php functions
returning objects i.e. pg_connect, mysql_result... a connection resource
is an object, a resultset is an object, then if you manipulate them or
return them in some functions, return the reference, not the copy.

another good idea to save your memory is to unset your objects once your
done with 'em, like one would do in C++ maybe.

.b
Jul 17 '05 #4
Randell D.:

Folks,

I've asked this before but never got any response but its important and I
thought I'd pitch it again (hopefully a bit clearer)...

One can pass data from one function to another either by passing a copy,
or passing by reference.

My understanding of passing by reference (putting the & before a variable
during the function declaration) means that the original data is used.

My understanding of *not* passing by reference, (thus passing a copy)
literally
means creating a copy of the data contained in my variables - thus
doubling the memory usage for this data... even if its just a temporary
while the function is being executed.

I *believed* (past tense) that one should only pass by reference if they
wanted the child function to change the data and pass the newer values
back to the parent function.

I'm now thinking that this is just a feature and not the only usage. I am
begining to think I should use "pass by reference" nearly all the time in
order to conserve memory (since I won't be creating copies of my variable
data in memory).

Would this be good practice? Am I right that passing by reference means
that my variable data is not duplicated thus using less memory than if I
had passed my variable data as a copy?


That's very risky business IMO. At some point you're bound to forget about
it, and change a variable that was passed by reference. This can lead to
lots of hard to catch bugs. It is generally accepted that pointers are a
bad thing, and they should be avoided whenever you can. Functional
programming languages go farthest in this respect, and this also makes them
easier to program.

Also note that the PHP engine has some tricks up it's sleeve to avoid
unnecessary copying (http://www.zend.com/zend/art/ref-count.php), but I'm
not sure if this works with argument passing as well.

All in all I don't think it's worth the hassle. If memory is a problem you
should instead find the memory hotspots and optimize them, possibly
rewriting them in C.

André Næss
Jul 17 '05 #5
Carved in mystic runes upon the very living rock, the last words of odel
of comp.lang.php make plain:
Reference is useful only for object.
That's not true. Passing by reference is useful if you want the function to
modify the variable you send to it.
(I'm not even sure you can reference a string for example).

Of course you can.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #6
Alan Little didn't write:
odel wrote:


but Alan Little wrote:
(I'm not even sure you can reference a string for example).

Of course you can.


You can pass all variables by reference.
What you can't pass by reference are constants:

<?php
function xyz(&$p1, &$p2, &$p3) {
// do nothing
}

$s = 'string';
$i = 7;
xyz($s, $i, 'ERROR HERE');
?>

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #7

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

Similar topics

14
3063
by: dumboo | last post by:
hi there i m little bit confused over the following problem, i have understood wt the following code is doing...but not able to get the actual techinical stuff.....i have had a lot of hot debate...
110
9812
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
5
7802
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
3
1871
by: TeejB | last post by:
Hi all, Quick question: If I have a function which populates a large array (ie. reading rowsets), is it better to pass in a reference to a variable to accept the data, or should I just create,...
10
13609
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
13
3000
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
6
2698
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
11
3333
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
2996
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
7331
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
7391
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
7501
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...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.