Coffeehouse Thread

10 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Returning multiple objects

Back to Forum: Coffeehouse
  • User profile image
    W3bbo

    Did a quick Google, couldn't find anything...

    So what's the best way for a function to return multiple objects?

    I've got a function I want to return an Array and an Integer... what's the best way?

    Ciao
    -W3bbo

  • User profile image
    rhm

    any particular language?

  • User profile image
    figuerres

    W3bbo wrote:
    Did a quick Google, couldn't find anything...

    So what's the best way for a function to return multiple objects?

    I've got a function I want to return an Array and an Integer... what's the best way?

    Ciao
    -W3bbo


    Well IMHO a "Function"  takes 0 to n parameters that it reads and does not modify.
    returns a "type" in .Net which can be a simple value type or a complex object.

    that's based on my early training in C and Pascal

    any function that modifies a param has "side effects" and they can be a problem -- can lead to bugs.

    and should be avoided unless no other clear option works.

    when I need to return a "complex result" I first check to see if my idea is wrong and could be re-stated to work as a normal function.

    if not then I return a Type / STurct that may just be a holder for n types

    that way you get one output but it can contain the details of the functions results.

    thats my general rule.

    if the outputs are an array and an int can you predict the dimensions of the array before the function is called?
    if not then return an object that has an array in it.

    if you know the array size then you may pass in the array and retun the int with the 1 side effect as a known and documented case.

    thats my way of dealing with that.

  • User profile image
    W3bbo

    Nevermind, Sven went through with it on messenger... turns out I need to use:

    Function Foo(ByRef Bar) As Zok

    Rather than

    Function Foo(ByVar Bar) As Zok

  • User profile image
    Yggdrasil

    figuerres wrote:
    any function that modifies a param has "side effects" and they can be a problem -- can lead to bugs.


    I'm less of a purist than figuerres is. Just pass two out params (ByRef in VB) and modify them inside the function.


    void myFunc (out int one, out string[] two)
    {
    one = 5;
    two = new string[] {"one", "two"};
    }

    void caller()
    {
    int one;
    string[] two;
    myFunc(out one, out two);
    // Params are now initialized.
    }


    And why isn't this in Techoff?

  • User profile image
    W3bbo

    Yggdrasil wrote:
    And why isn't this in Techoff?


    The average response time in Techoff is about an hour Wink

  • User profile image
    Manip

    Well I'm sorry but if I post my topics in techoff then everyone else should have to as well.

  • User profile image
    geekling

    Really depends on what language you're using.

    In Boo, you'd return a tuple:

    def MyFunc(input)
    /*...*/
         return val1, val2

    return1, return2 = MyFunc(input)

    Or you could pass by ref as other people suggested, or return a complex data type such as a structure created in the function. Wink

  • User profile image
    Maurits

    It really depends on your application.  If you're writing a complex number library you'd really want to use a custom type which had two floats for real/imaginary parts, then have your functions return this custom type rather than messing with input/output parameters.  Or if you find yourself passing a particular object around all the time you could make it a "has-a" member of your class - or even descend your class as an "is-a".  Input/output parameters are also appropriate so long as it is clear to the caller that their passed parameter isn't read-only.

  • User profile image
    DoomBringer

    You could make a simple little struct to hold the data in, but passing stuff in by ref is the usual practice...

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.