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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ariel White

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

    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.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      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.
      Last edited by whodgson; Oct 22 '10, 05:27 AM. Reason: fix a typo

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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.

        Comment

        • Gauarv Kumar
          New Member
          • Oct 2010
          • 10

          #5
          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

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            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

            • Gauarv Kumar
              New Member
              • Oct 2010
              • 10

              #7
              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

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                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

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  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

                  Working...