C# Gamedev Beginner Tips: Two easy ways to return multiple values from a function

Imagine this situation, you’re working on a Turn-Based strategy game, and you want to show the lowest and maximum damage one unit will deal to another. You know a single function can’t return multiple things, so you write two like this:

int GetLowestDamage(int attackValue)
{
//Does some stuff
}
int GetHighestDamage(int attackValue)
{
//Does some similar stuff
}
void ShowDamageSpread()
{
lowDamage.Text = enemy.GetLowestDamage(myAttack);
highDamage.Text = enemy.GetHighestDamage(myAttack);
}

Great. Good job! But both of these functions do very similar things. In fact there might even be some copy-pasted code between them. Wouldn’t it be great if we could combine these two, returning both the upper bound and the lower bound at once, doing the single calculation and returning both results. Well, actually there are (at least)TWO ways of doing it.

Using an out parameter

out parameters are the fastest way to get multiple results returned from a function. Here’s a quick example of how the code sample above would look rewritten using one function and out parameters:

void GetDamageSpread(int attackValue, out int lowDamage, out int highDamage)
{
//DoStuff
lowDamage = //LowDamageAmount
highDamage = //HighDamageAmount
}
void ShowDamageSpread()
{
int lowDam;
int highDam;
enemy.GetDamageSpread(myAttack, out lowDam, out highDam);
lowDamage.Text = lowDam;
highDamage.Text = highDam;
}

One thing you’ll notice here, is that you need to declare your variables before you make the call to your function. Additionally, you must assign a value to all out parameters before you return. 
For those of you curious about what’s going on in here, marking a parameter as out passes it into the function by reference, instead of by value, similar to the “ref” modifier. It’s a quick and easy way to get extra things out of a function, although does run the risk of making your code lessreadable. So here’s an alternative

Using a class

You might think that declaring a whole class just to act as a result value container is overkill, but actually it’s a very object oriented way of dealing with this “multiple-output” problem. More than likely, if the function is returning two values, those values are related to each other in some way and could benefit from always being accessible from the same object. So, code sample:

private class RangeSpread
{
public int lowDamage;
public int highDamage;
}
RangeSpread GetDamageSpread(int attackValue)
{
//DoStuff
RangeSpread result = new RangeSpread();
result.lowDamage = //LowDamageAmount
result.highDamage = //HighDamageAmount
return result;
}
void ShowDamageSpread()
{
RangeSpread damageSpread = GetDamageSpread(myAttack);
lowDamage.Text = damageSpread.lowDamage;
highDamage.Text = damageSpread.highDamage;
}

So some tips for when you’re using classes. Firstly, you can declare a class within a class, and mark it as private. This is really useful if you’re only planning to use the return value within the current scope, so you don’t have access to a load of useless classes elsewhere. Also, remember this return value is a class, meaning you could define a few functions to handle common things you might want to do with the values it contains! For example:

private class RangeSpread
{
public int lowDamage;
public int highDamage;
public int GetMidDamage()
{
return (lowDamage+highDamage)/2;
}
}

I hope you’ll excuse these extremely contrived examples! Generally speaking, using classes will leave you with neater looking, and better organised code. However, in one-offs and for a quick and dirty fix, out parameters have your back!

If you found this interesting or useful, be sure to hit that recommend button to share it out to others, and continue the discussion about how you use techniques like these.

Nathan John is an indie developer currently working on WarpBall and launching the website Instant Game Coder (where this article was originally posted)