Thread: Call-by-value Vs. Call-by-reference

  1. #1
    Registered User
    Join Date
    Feb 2002

    Call-by-value Vs. Call-by-reference

    Just something I'm sure everybody knows except for me

    What is the difference between thses two (call-by-value & call-by-reference) and which one is better, which one should I use.

    I kinda understand I concept, at least I think I do anyways.
    To me there dosn't seem to be much difference. I don't know!

    Thanks guys! Help me out pleaseeeeeeeeeeee

  2. #2
    Registered User
    Join Date
    Nov 2001
    by value uses the value of a variable with a new variable name, but the other just assigns a pointer that changes the value of the original variable automatically

  3. #3
    Registered User
    Join Date
    Feb 2002
    Ok are these the same as local variables or are these only encountered with functions and classes...

    I'm getting the feeling they are encountered when functions are called only... I don't know...

    Are they?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    #include <iostream>
    using std::cout;
    using std::endl;
    void ByRef(int& x){
     x *= 10;
     cout << "x in ByRef() = " << x << endl;
    void ByVal(int x){
     x *= 10;
     cout << "x in ByVal() = " << x << endl;
    int main(){
    int x = 10;
    cout << "Before ByVal() x = " << x << endl;
    cout << "After ByVal() x = " << x << endl;
    cout << "**x has not changed in the main function**" << endl;
    cout << "After ByRef() x = " << x << endl;
    cout << "**This time x has changed**" << endl;
    return 0;
    Notice here that the two functions are almost identical and are used the same, but ByRef() recieves the int as a reference.

    What this means is that the passed value is passed by reference, and so any changes to the value in ByRef() are reflected by the same changes to the value in the main function.

    With ByVal(), a copy of the int is sent, and so any changes made to it are not reflected in the value held in the main function.

    Passing by reference can be troublesome as it can screw up the variable unintentionally. To combat this if you dont want the value changed, you would declare the recieved value in the function to be constant with the "const" keyword.

    You may wonder why bother sending by reference at all.....the benefit is that with send by reference, the recieved value is an alias for the sent value and so therefore is more efficient as a whole value does not need to be passed. (this is here nor there for an int, but does matter with large objects).

    Hope that helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. call by reference
    By Jackie in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 06:46 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM