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.
What does it mean to pass data by value compared to passing data by reference?
Collapse
X
-
Ariel WhiteTags: None -
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. -
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.Comment
-
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.Comment
-
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.Comment
-
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).Comment
-
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.Comment
-
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++.Comment
Comment