By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

What does it mean to pass data by value compared to passing data by reference?

P: n/a
What does it mean to pass data by value and how does that compare to passing data by reference? Please explain to me why we would pass some data by value and others by reference.
Oct 21 '10 #1
Share this Question
Share on Google+
8 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You pass by reference to avoid making a copy of the object for the function to use.

You pass by pointer to avoid making a copy of the object for the function to use.

Therefore, you use refernces as arguments to avoid using pointers.
Oct 21 '10 #2

100+
P: 542
Yes...and passing by value does not allow you to change the original value it only allows you to change the copy. Passing by reference on the other hand allows you to change the value of the object in memory which is often required.
Try using the swap function using by value and by reference and you will see the difference.
Oct 22 '10 #3

Expert 100+
P: 2,396
Strictly speaking, C only supports pass-by-value. If a function has a pointer argument then that argument is passed by value. However, a pointer value allows you to reference the thing being pointed at.
Oct 23 '10 #4

P: 10
Pass by reference is used if you what to change the original variable and pass by value is used if you didn't what to change the original variable.

PASS BY REFERENCE IS FASTER BECAUSE IT INVOLVES LESS OVERHEAD.
Oct 23 '10 #5

Expert 100+
P: 2,396
Gauarv Kumar wrote "PASS BY REFERENCE IS FASTER BECAUSE IT INVOLVES LESS OVERHEAD."

Sometimes it is faster, sometimes it is slower. The time spent in parameter-passing overhead depends on the size of the parameter; in general, it takes more time to pass larger parameters, but there are lots of exceptions.

If the thing being referenced is smaller than a pointer then it will usually (but not always) be faster to pass it by value than to pass it by reference. Similarly, if it is larger than a pointer then it will usually (but not always) be faster to pass it by reference than to pass it by value.
Oct 23 '10 #6

P: 10
I got your point for example it will be better to pass single character in c by value.Because char type is 2 byte and a pointer to it will be of 4 byte(unsigned integer).
Oct 24 '10 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
That's not what donbock meant.

You pass a char by reference if the function needs to change the value in the original char. Otherwise, you pass the char by pointer.

You pass the char by pointer iof the function needs to point to another char. References can't be changed to refer to something else.

You try, always, never to pass a user-defined-type so that it makes a copy. Use a reference or a pointer based on the above two rules. Use const in the function argument to control access to the members of the UDT.
Oct 25 '10 #8

Expert 100+
P: 2,396
Actually, weaknessforcats has a generous interpretation of what I wrote. I was responding to the specific point about call-by-reference always being faster than call-by-value. My post explained that sometimes call-by-value is faster than call-by-reference; and I hinted that it can be difficult to reliably predict which will be faster.

I did not question the wisdom of making execution speed the sole design constraint. Let me go on the record: my opinion is that in almost all cases, achieving clear and portable source code is more important than optimizing time or space efficiency -- I believe in maximizing maintainer efficiency. That said, there is nothing with improving time or space efficiency as long as you don't hurt maintainability.

Also, I'm an unrepentant C programmer. I believe weaknessforcats' distinction between call-by-pointer and call-by-reference is only meaningful in C++.
Oct 25 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.