Thread: pass be reference versus pass by value

  1. #1

    pass be reference versus pass by value

    Hello all,

    I am new to C++, and have a quick ?, I am having trouble understanding what is the diffrence between pass by reference and pass by value??

    I've read pass by reference is better as it'll make a copy of the parameter so that the orginal will not be altered, but by value will allow the the original to be altered?? it this correct or do I have it the other way around?? I'm just not getting this concept?? could someone please explain( and which one is more dangerous), I really new to C++ so want to make sure I completely understand all the basic concepts.....

    also, by reference: int function (int parameter)?? is this correct with the &....

    many thanks

  2. #2
    Registered User
    Join Date
    Aug 2001


    My friend. You have that backwards. Passing by reference will allow altering of the original. Where as pass by value makes a copy...

    pass by reference sample
    void MultiBy2( int& iValue)
           iValue = 3 * 2;     //value will exist out side of function  
    pass by value
    int MultiBy2( int iValue )
          iValue = iValue * 2;
          return iValue; //iValue returned and must be captured in variable i.e. int myValue = MultipBy2( 2 );
    The important thing is when you have large objects that are not feasible to pass by value such the contents of large string because when you pass by reference you are passing the 32 bit address instead of the values within the object.
    You can also prevent modification when passing by reference by adding the key word const i.e. const int& iValue......
    This enables passing large objects by reference for read only purposes... Make sense?

  3. #3
    thanks to both cleared it for me.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. how can i pass by reference by "malloc 2d array"?
    By Mathsniper in forum C Programming
    Replies: 10
    Last Post: 05-22-2005, 02:23 PM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM