C
C#2y ago
alex_aom

How to take 2 outputs from a function

I can't work out how to take 2 outputs from a function (3rd line isn't valid)
int i;
int j;
[i,j] = randomStart(MATRIX_ROWS, MATRIX_COLUMNS);

int[,] randomStart(int MATRIX_ROWS, int MATRIX_COLUMNS)
{
int randStart;
int x3;
int y3;
randStart = Random.Range(0, 3);
switch (randStart)
{
case 0:
x3 = 2;
y3 = Random.Range(1, MATRIX_COLUMNS - 1);
break;
case 1:
x3 = 9;
y3 = Random.Range(1, MATRIX_COLUMNS - 1);
break;
case 2:
x3 = Random.Range(1, MATRIX_ROWS - 1);
y3 = 2;
break;
case 3:
x3 = Random.Range(1, MATRIX_ROWS - 1);
y3 = 9;
break;
}

int[,] output = new int[x3,y3];
return output;
}
int i;
int j;
[i,j] = randomStart(MATRIX_ROWS, MATRIX_COLUMNS);

int[,] randomStart(int MATRIX_ROWS, int MATRIX_COLUMNS)
{
int randStart;
int x3;
int y3;
randStart = Random.Range(0, 3);
switch (randStart)
{
case 0:
x3 = 2;
y3 = Random.Range(1, MATRIX_COLUMNS - 1);
break;
case 1:
x3 = 9;
y3 = Random.Range(1, MATRIX_COLUMNS - 1);
break;
case 2:
x3 = Random.Range(1, MATRIX_ROWS - 1);
y3 = 2;
break;
case 3:
x3 = Random.Range(1, MATRIX_ROWS - 1);
y3 = 9;
break;
}

int[,] output = new int[x3,y3];
return output;
}
4 Replies
Thinker
Thinker2y ago
Do you actually want to return a 2D array, or two separate values?
alex_aom
alex_aomOP2y ago
I want to get 2 seperate integers as the output
Thinker
Thinker2y ago
You probably just want a tuple.
var (i, j) = RandomStart(...);
var (i, j) = RandomStart(...);
(int, int) RandomStart(...)
{
// ...

return (x3, y3);
}
(int, int) RandomStart(...)
{
// ...

return (x3, y3);
}
alex_aom
alex_aomOP2y ago
Time to read up on tuples, thanks haha
Want results from more Discord servers?
Add your server