Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

return more than 1 value

Discussion in 'Scripting' started by Shonysky, Apr 7, 2015.

  1. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    //C# CODE//

    how can i return more than 1 value?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    Could return custom class/struct or array..
     
    Kiwasi likes this.
  3. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    In what context?
    But what mgear said.
    Additionally, there's also the out parameter.
     
  4. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    i would like to return 3 values that i have in my function

    i was trying to do

    return value1 & value2 & value3;

    but that doesn't worked aswell

    how can i return in an array?
     
  5. Deleted User

    Deleted User

    Guest

    Can you post the code you're thinking of using?
    You can usually use .ToArray() or an IEnumerator to return multiple values.
    You can also specify array in return type like this:

    Code (CSharp):
    1. int[] Foo(int a, int b, int c){
    2. return new int[3] = {a, b, c};
    3. }
     
    Kiwasi likes this.
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Unfortunately, C# doesn't support the concept of multiple return values per se. However, there are several ways of passing extra information out of a function that might help you, depending on your situation.

    Option #1: Return one complicated variable that contains all your data
    Instead of returning a simple type like "int" or "bool", you could return a more complicated type such as an array, List, Dictionary, or even a custom struct or class that contains whatever data you need.

    Code (CSharp):
    1. public struct myComplicatedReturnType {
    2.     int myInt;
    3.     bool myBool;
    4.     string myString;
    5. }
    6.  
    7. myComplicatedReturnType  myFunction() {
    8.     // Some code that creates and returns the appropriate struct
    9. }
    Option #2: Out (or Ref) Parameters
    When you declare the arguments for your function, you can put the "ref" or "out" keyword in front of them to modify the way they're passed. A "ref" parameter is passed by reference, so if the function modifies the value of that variable, the variable is also changed for the caller. An "out" parameter is like a "ref" parameter except that it doesn't even have to be initialized before calling the function (you're saying that the function doesn't care about its initial value; it exists solely to pass information back out of the function).

    Code (CSharp):
    1. bool myFunc(out int number) {
    2.     number = 5;
    3.     return true;
    4. }
    5.  
    6. void otherFunc() {
    7.     int x;
    8.     bool b = myFunc(x);
    9.     // x now has the value 5 (and b has the value true)
    10. }
    Option #3: Pass handles as parameters, then modify the objects they reference
    One drawback of ref/out parameters is that they can't be optional; if they're in the function definition, the caller must provide a variable for each of those parameters, even if they don't care about the result.

    But if you pass a class as just a normal "in" parameter to a function, you're really passing a handle to the class, not a copy of the entire instance. So any modifications to that object made inside the function will show up outside it.

    Code (CSharp):
    1. using System.Collections.Generic;
    2.  
    3. void myFunc(List<int> data = null)
    4. {
    5.     if (data != null) {
    6.         data.Add(5);
    7.         data.Add(17);
    8.         data.Add(-2);
    9.     }
    10. }
    11.  
    12. void otherFunc()
    13. {
    14.     List<int> myList = new List<int>();
    15.     myFunc(myList);
    16.     // myList now contains 5, 17, and -2
    17. }
    Option #4: IEnumerable<T>
    This isn't really a "general" solution to the problem of multiple return values, but there is a built-in language feature to return multiple values in one special case, which is when you're dealing with iterators. You can return values "one at a time" using yield return, and then the caller can iterate through them with a foreach loop or manipulate the collection in a bunch of interesting ways using Linq.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3.  
    4. IEnumerable<int> myFunc()
    5. {
    6.     for (int i = 0; i < 10; ++i) {
    7.         yield return i*5;
    8.     }
    9. }
    10.  
    11. void otherFunc()
    12. {
    13.     foreach (int x in myFunc()) {
    14.         Debug.Log(x);    // Logs 0, 5, 10, 15, etc.
    15.     }
    16.     int[] myArray = myFunc().ToArray();  // Creates an array containing 0, 5, 10, etc.
    17. }