Function retuning incorrect value

This is a discussion on Function retuning incorrect value within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm revising for my intro to computing class short-test two, and going through a previous year paper, I ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    22

    Function retuning incorrect value

    Hi all, I'm revising for my intro to computing class short-test two, and going through a previous year paper, I attempted to solve this function on paper, and the answer I got on paper for a was 6, but when I run the program and print a, it yields a value of 3. What am I doing wrong here? Please and thank you, any feedback is welcomed.
    Code:
    void foo (int x, int & y)
    {
    int c;
    c = x + y;
    y = 2 * c;
    x = 2 * x;
    }
    the exact question was this

    In the main program, if you have the following code:
    int a = 3, b = 3;
    foo(a, b);

    What is the value of the variable “a” after “foo” is called (and executed) in the main program as above

    What exactly am I doing wrong here?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    Your mistake is in thinking that a is modified from within foo. Rather, the parameter x holds a copy of the value of a. The value of a itself is not changed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    So if x holds a copy of the value of "a", then why is the value of "y" changed? Is it due to the fact that, "y" is passed by reference? But if both values go into foo, then shouldn't both values be changed?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    Quote Originally Posted by cool_guy
    So if x holds a copy of the value of "a", then why is the value of "y" changed? Is it due to the fact that, "y" is passed by reference?
    Yes.

    Quote Originally Posted by cool_guy
    But if both values go into foo, then shouldn't both values be changed?
    The difference is that the reference parameter y becomes an alias for the variable b, so changes to y are reflected in b.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    Oh, I think I understand now. IF a function is called and if the parameter is passed by reference, then any changes to the values that are passed, also affect the real variable?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by cool_guy View Post
    Oh, I think I understand now. IF a function is called and if the parameter is passed by reference, then any changes to the values that are passed, also affect the real variable?
    Easy said (and if I am right) a value not passed in is assigned to a local variable and is only available inside the function.

    Your variable a is a local variable inside the main function and has no meaning in your function foo. In there a new variable called a is created when you try to use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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