Thread: A question about return statement

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Mysore, India
    Posts
    14

    A question about return statement

    Hi,

    I've a function which accpets two integer parameters. Now, after doing some processing within the function I want the two values back. How do I do that? Will return statement be helpful?

    Thanks,
    Babu

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    You can send the pointers to the two integers to your funciton. The values will get modified and the changes are also reflected for the rest of the program.
    In the middle of difficulty, lies opportunity

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If by "I want the two values back" you mean that you will change the values of the arguments and want that to be reflected in the caller, then perhaps you should pass them as pointers.
    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

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

  5. #5
    Registered User
    Join Date
    Jun 2007
    Location
    Mysore, India
    Posts
    14
    Yes laserlight, that's what I meant. But why is it not getting reflected when I pass them as values and not the reference ?

  6. #6
    Registered User
    Join Date
    Jun 2007
    Location
    Mysore, India
    Posts
    14
    Thanks jverkoey! I got it now

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Think of it in the piracy sense: if you want a movie these days, you copy it from someone else on the internet and now you have your own local copy of the movie. With this new copy you are free to do whatever you want, and the person who gave it to you doesn't really care or for that matter know about it. This is pass by value.

    Flash-back 15 years ago when people borrowed movies from each other: if you accidentally overwrite that ET cassette with your favorite episode of Home Improvement your friend is going to know. This is pass by reference.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Location
    Mysore, India
    Posts
    14
    wow! That was an excellent analogy! Thanks a lot

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's C, no passing by reference

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    11
    "jverkoey" nice explaination...

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by zacs7 View Post
    It's C, no passing by reference
    The term is still used in C, although the semantics are different when compared with C++. Since C++ references are really 1337ified pointers that aren't pointers and that have special properties to them, it's all really the same under the hood.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM