Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I decide what a function should return?

Quick answer

Return a value when the caller needs one result to use or inspect. Return several related values when they form one coherent result; in C#, a named tuple is one option. Use void when the function’s purpose is to perform work and the caller does not need a result. Ask what the caller needs next, and avoid returning unrelated items together just to reduce the number of functions.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

How do I decide what a function should return? I keep hesitating between a value, several values, and void.

Geez

Start with the caller’s next step: what does it need from this function to continue?

  1. If it needs one result, return that value.
  2. If it needs a few results that belong together, consider returning them together.
  3. If it only needs the function to do something, and has no result to use, void may fit.

In C#, a method declares a return type or void; a non-void method returns a value, while a void method can perform work without returning one. C# also supports returning related values as a tuple, including named elements. [S1] These are available choices, not rules that decide the best design for every method.

Follow-up question

What would returning one value look like, and when is that clearer than returning more?

Geez

Use one return value when one answer is the natural result of the operation. For example, suppose a method computes a subtotal from a price and quantity:

static decimal CalculateSubtotal(decimal price, int quantity)
{
    return price * quantity;
}

The caller can use the result directly:

decimal subtotal = CalculateSubtotal(12.50m, 3);

The hypothetical values here are just an illustration. The method’s stated purpose and its returned value line up: it calculates a subtotal, and the caller receives that subtotal. If a caller also needs some other distinct piece of information, don’t squeeze it into the subtotal or make it guess what the result means. Decide whether that information is genuinely part of the same result.

Follow-up question

When does returning multiple values make sense? Could I return a subtotal and the quantity used?

Geez

A tuple can return multiple related values, and named tuple elements can make the parts easier to identify at the call site. [S1] Use that option when the values describe one coherent result and the caller is likely to need them together. For example, if a hypothetical calculation reports both a subtotal and the quantity used:

static (decimal Subtotal, int Quantity) CalculateSummary(
    decimal price, int quantity)
{
    return (price * quantity, quantity);
}

var summary = CalculateSummary(12.50m, 3);
Console.WriteLine(summary.Subtotal);
Console.WriteLine(summary.Quantity);

Here the values are named rather than left as unexplained positions. But first ask whether Quantity is useful output or merely an input the caller already has. If the caller already knows it and does not need it returned, a single subtotal may be simpler. The documentation establishes that C# supports tuple returns; it does not establish that tuples are always preferable, or prescribe which values belong together. [S1]

Follow-up question

And when should I use void instead? I’m unsure whether a method that saves something needs to return a value.

Geez

Use void when the method’s job is an action and the caller does not need a result from it. For example, a method that displays a supplied message could be:

static void ShowMessage(string message)
{
    Console.WriteLine(message);
}

The caller asks it to perform the action; there is no computed message value to assign. C# void methods can perform work without returning a value, and a bare return can stop their execution. [S1]

For a save operation, decide what the caller needs to know afterward. If it needs a result, choose and define that result rather than returning an arbitrary value. If it only needs the action performed, void can be appropriate. The documentation describes the language’s return options, but does not specify what a particular save method should report or how saving should work. A small decision checklist helps keep that choice tied to your caller’s needs.

A practical checklist

  • Write down what the caller needs to do immediately after calling the method.

  • Return one value if there is one clear result the caller needs.

  • Consider a named tuple only when the returned values are related and useful together.

  • Use void if the method performs an action and the caller needs no result.

  • Check that names and the call site make each returned value understandable.

  • Reconsider the design if the method returns unrelated values or repeats information the caller already has.

A few more useful answers

Is returning a tuple always better than creating a separate type?

No. C# supports returning related values as tuples, but that does not make tuples the right choice for every design. Consider whether the result needs a more descriptive structure, and keep the decision focused on what the caller needs.

Can a `void` method stop before reaching its end?

Yes. In C#, a void method can use a bare return to stop execution. That ends the method without returning a value. [S1]

What if the method has an action and a useful result?

Make the caller’s needed result explicit and decide whether it belongs with the action’s outcome. The language’s available return forms do not, by themselves, determine the right design for a particular method.

Sources

References used for this guide.

  1. [S1] Overview of methods — C# | Microsoft Learn
    learn.microsoft.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work