Thread: return values

  1. #1
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25

    return values

    is it possible to return more than one value?
    im writing a prog where i send a value to a function but i want that function to return 4 integers. Will it be easier/wiser to use global variables??
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    NO! But you can use array, and then return the pointer to the first element of the array.

  3. #3
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    so i store those 4 integers in an array,return the pointer to the array and use a loop to print them,right?
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    The array should be defined out of the function which is called. You can define the array inside function main or somewhere else.

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    The array should be defined out of the function which is called. You can define the array inside function main, or somewhere else.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I wouldn't do this with an array. Having the caller pass an array is clumsy (eg an opportunity for the caller to pass an array that is too short). Using a global arrays imposes some limitations on callers (eg the function can't be called multiple times between two sequence points, and often cannot be recursive). Having the function dynamically allocate the array to return as a pointer is an opportunity for a memory leak.

    My prefered way would be to return a structure (or a class), eg
    Code:
    struct X
    {
        int a,b c,d;
    };
    
    X function()
    {
           X x;
           // set members of x
           return x;
    }
    I'm assuming you don't want to simply pass four arguments by pointer or reference.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, you could return a vector of integers.
    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

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    how about a struct of stuff
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    In this particular case you wind up copying the structure (some optimizing compilers are smart enough to avoid this, though). If, on the other hand, you pass in a pointer to the struct (or a reference to the class) then you avoid the potential for the copy.

    In case someone things to return a pointer to the local value of x, that is dangerous and will inevitably lead to errors and debugging nightmares as x is allocated on the stack and will be overwritten. The use of a static instance will work as long as you do not require reentrancy (for recursive functions and/or multi-threading), but it is best to simply pass in a pointer/reference.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  10. #10
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    You guys lost me. I think im to much of a novice to understand. Will anybody show me the way to become a good programmer??
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  11. #11
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Just keep on keepin on. I have probably read close to 10,000 pages of computer science related material and have been a professional programmer for at least a decade (actual calculation will make me feel too old to justify the effort). I didn't have forums like these when I was getting started, they go a long way to helping get through the learning curve.

    WRT the copying, when a function returns a value that value is copied to a location on the stack. If the calling function actually uses the return value, it is typically copied from the stack to the memory location given in the calling function. For a couple of ints, that is not a big deal, but C++ objects can get huge fast and if you are doing this in a tight loop you can wind up spending 99% of your time copying return values around.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I wouldn't worry about the efficiency of copying at this point. Just use grumpy's method (except for the missing comma).

  13. #13
    Banned
    Join Date
    Jun 2005
    Posts
    594
    couldnt you just send the addresses of a variable(s) and change
    it int he function and return nothing while still getting the
    information.

    Code:
     
    void function(int &a, int &b)
    {
        //do something to a and b
    }
    im not actually fimilar with doing this, and i dont know
    if memory leak can occur with this, but ive seen it discussed
    a few times before so i might be a decent solution?

  14. #14
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    No memory leak here, this is an excellent way to handle the issue. The struct/class method just makes packaging easier, potentially making maintenance easier, that is all.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by grumpy
    an opportunity for the caller to pass an array that is too short
    that's really a caller responsability

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. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM