Thread: Function retuning incorrect value

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 06: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