best way for a function to return multiple value

so I have a function that needs to return multiple values of type int. but I’m not sure the best way to return the values. the issue being that the number of value is random and could be anywhere between 1 to 1000 int’s.

initially the ints are in an Arraylist but I’m not sure if returning one is possible or the syntax of the code that would call said function

any suggestions or examples would be great
in c#

1 Like

I don’t see any reason you could noy return an ArrayList. Proceed like that.

if you use C#, I would recommend using its standard way: defining parameters in the function declaration as out and passing them in for actual retrieval

that way you can get as many returns as you want

void SomeFunction( out int return1, out string return2 )
{
  return1 = 7;
  return2 = "trala";
}

void Start()
{
  int i;
  string a;

  SomeFunction( out i, out a );
  print( string.Format("{0} found {1} noodles", a, i.ToString()) );
}
28 Likes
using UnityEngine;
using System.Collections;

public class ArrTest : MonoBehaviour
{
    ArrayList arr = new ArrayList();

    // Use this for initialization
    void Start()
    {
        arr = GenerateArray();
        string s = string.Empty;

        for (int i = 0; i < arr.Count; i++)
        {
            s += "Random int: " + arr[i].ToString() + System.Environment.NewLine;
        }

        Debug.Log(s);
    }

    // Update is called once per frame
    void Update()
    {

    }

    private ArrayList GenerateArray()
    {
        ArrayList arr = new ArrayList();

        for (int i = 0; i < 10; i++)
        {
            arr.Add((int)(1000 * Random.value));
        }

        return arr;
    }

}

The OP stated that the number of return values will vary, and that there may be as many as 1000.

@The OP: There should be no problem returning a container of values (e.g. an array, ArrayList, List<>, etc.). See scarpelius’ post for an example.

12 years later and this post still helped me.

5 Likes

I don’t get the use case actually. If you need your function to return a variable number of parameters, it’s usually the sign that you should refactor the code. If you need to return a variable number of integers, just have your function return a List.

@Magnesium : ofcourse list can be used. However, for that you will have to create a custom class list and return that or else create multidimensional list. But for example your function calculates two values A and B and you simply want to return that without hassle of doing all this, you can use the above method. I used it and was quite easy. I may be wrong in my observations, as I am no expert on this topic.

Worst case, if you need to return some complicated arrangement of data, simply create a custom class that contains all the variables you need (not a MonoBehaviour class in this case), and return an instance of that class.

Another option for returning extra variables is the “out” parameter modifier.

You can see an example of how that is used in some of the overloads for Physics.Raycast.

edit: Sorry, just noticed “out” was already discussed above. I should read more, type less :slight_smile:

And Custom class as well :slight_smile:

But its always fun to learn more…

1 Like

Dreamora, Big Thanks to you :))) Your post really helped me.

A simpler approach since C# 7 is to use the tuples pattern:

Example:

(string, string, string) LookupName(long id) // tuple return type
{
...
// retrieve first, middle and last from data storage
return (first, middle, last); // tuple literal
}

There is more information here:

6 Likes