Course contentsShow
← Computer Science
Lesson 200 of 2,8725. Functions and ModularityFree lesson

Multiple Return Values

Learn techniques for returning multiple values from a function using tuples, structures, or output parameters.

Multiple Return Values

You'll learn how to return more than one piece of information from a single function call.

Sometimes a function needs to give back multiple results at once. Imagine a function that finds both the smallest and largest number in a list — you want both answers, not just one. Most programming languages have ways to package multiple values together so they can travel back from a function as a set.

Why Return Multiple Values?

When a function performs a calculation that naturally produces several related results, returning them together is cleaner than calling separate functions. For example, a function that divides two numbers might return both the quotient and the remainder.

Common Techniques

Tuples: Many languages let you bundle values into a lightweight container called a tuple. The function packs values together, and the caller unpacks them.

Structures/Objects: You can define a custom data type that holds multiple fields, then return one instance of that type.

Output Parameters: Some languages allow "output" or "reference" parameters — variables passed to the function that it modifies directly, effectively "returning" values through them.

Conceptual Example

Think of a function like a vending machine. Normally it dispenses one item (one return value). But imagine a special machine that, when you press a button, gives you both a snack and a drink in a small carrier bag. The bag is like a tuple — it keeps the items together during delivery.

Key Takeaway: Multiple return values let a single function deliver several related results efficiently by packaging them together using tuples, custom structures, or special parameters.