Thread: function

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    function

    Is it possible to have a function that that takes and returns multiple variables other than arrays?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, you can pass arguments as references.

    Or return a struct / class.

    Then there are also standard containers which probably wouldn't count as an array.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    A C or C++ function can only "return" one value, declared as the return value in the function prototype. In reality, you can pass arguments as pointers (or by "reference" if you prefer C++ nomenclature) to local variables which will take on a value after the function returns.

    Ie:

    Code:
    void area(int length, int width, int *area)
    {
      *area = length * width;
    }
    I'm a little shaky on C++ code but you could do the same using references...

    Code:
    void area(int L, int W, int &Area)
    {
      Area = L * W;
    }
    I'd think either would work...

    SOrry for the C++ code. In C++, the & operator can have a different meaning depending on context.

    Just check out the first example...
    Last edited by MacNilly; 07-23-2007 at 02:36 AM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by MacNilly View Post
    A C or C++ function can only "return" one value, declared as the return value in the function prototype. In reality, you can pass arguments as pointers (or by "reference" if you prefer C++ nomenclature) to local variables which will take on a value after the function returns.

    Ie:

    Code:
    void area(int length, int width, int *area)
    {
      *area = length * width;
    }
    I'm a little shaky on C++ code but you could do the same using references...

    Code:
    void area(int L, int W, int &Area)
    {
      Area = L * W;
    }
    I'd think either would work...

    SOrry for the C++ code. In C++, the & operator can have a different meaning depending on context.

    Just check out the first example...
    Better still, forget the first method, and go with the second. Its better to avoid pointers in C++ and use references where possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM