Follow up question re Calling by Value vs Reference

This is a discussion on Follow up question re Calling by Value vs Reference within the C++ Programming forums, part of the General Programming Boards category; Hi All, another easy one for you guys... I'm trying to figure out why I'm receiving the same results after ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    Cool Follow up question re Calling by Value vs Reference

    Hi All, another easy one for you guys...

    I'm trying to figure out why I'm receiving the same results after executing both functions in the following program. Shouldn't the bolded line produce a result of 15?

    Thanks for any help...
    _________________

    int tripCBV(int);
    int tripCBR(int &);

    int main()
    {
    int count = 5;

    cout << "BEFORE - CBV result is " << count << endl;
    cout << "AFTER - CBV result is " << tripCBV(count) << endl;
    cout << "AFTER function result is " << count << endl << endl;

    cout << "BEFORE - CBR result is " << count << endl;
    cout << "AFTER - CBR result is " << tripCBR(count) << endl;
    cout << "AFTER function result is " << count << endl << endl;

    return 0;
    }

    int tripCBV(int x)
    {
    int y;

    y = 3 * x;
    return y;
    }

    int tripCBR(int &x)
    {
    int y;

    y = 3 * x;
    return y;
    }


    cout << "AFTER function result is " << count << endl << endl;

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You never modify the value of x in either of the functions.
    If you just had 'return 3*x;' you'd see the difference.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    You never changed the value...

    //beaten, o well. Also, code tags are a wonderfull thing

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    I still don't see it...

    Thanks to both of you.... still need help here. I know this is very simple which is why I'm getting frustrated here. You both mention I never changed the value... isn't that what I was doing within the functions?

    I also changed my return statement to 'return 3 * x;' which did not change anything... I wouldn't have understood if it did though...

    Any other suggestions/help to lead me in the right direction here???

    Thank you for dealing with me...

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: I still don't see it...

    >>You both mention I never changed the value... isn't that what I was doing within the functions?
    Look at this:
    Code:
    int tripCBR(int &x)
    {
      int y;
    
      y = 3 * x;
      return y;
    }
    When do you assign a new value to x? You don't is the answer.

    >>I also changed my return statement to 'return 3 * x;'
    This also doesn't change x.

    Do something like
    x *= 3;
    which is shorthand for
    x = x * 3;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    //int tripCBR(int &x)
    this shouldn't be int *x?

    //y = 3 * x;
    shouldn't be y = 3 * (*x)?

    And this won't change x...
    Code:
    void tripCBR(int *x)
    {
      *x *= 3;
    }
    This should...
    Last edited by Vber; 03-16-2003 at 05:36 PM.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    GOTCHA!!!

    THANKS!!!

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>//int tripCBR(int &x)
    >>this shouldn't be int *x?
    You've changed it to a pointer now, it was by reference first. Maybe have a read of this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ohh god damn, this question is in the C++ board, because this I see you guys didn't tell him what I wrote, in C there is no references right? well I can pass a variable as reference and not by val, but with Pointers.

    Sorry sorry sorry sorry
    I tought this was on C board! My Mistake sorry!!!

    It was wierd to see the & on the function prototype, hell I don't know C++

    Sorry again, my foul.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    > also changed my return statement to 'return 3 * x;' which did not change anything...
    Sorry, wasn't thinking. Should've been 'x *= 3; return x;'.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

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, 09:18 PM
  2. non-const reference and const reference
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2007, 05:03 AM
  3. Gnarly Linking Error HELP!!!!
    By brooksbp in forum C++ Programming
    Replies: 8
    Last Post: 05-04-2007, 02:00 AM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM
  5. Dll question again. Reference to No-One
    By G'n'R in forum C Programming
    Replies: 0
    Last Post: 10-24-2001, 04:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21