Returning Multiple Values
Option 1: return a struct ✅ (clearest)
struct Stats { double min, max, average; };
Stats compute(const std::vector<double>& v);
Stats s = compute(data);
std::cout << s.average;
Named fields = self-documenting.
Option 2: std::pair (<utility>)
std::pair<int, int> min_max(...);
auto p = min_max(v);
p.first, p.second
Option 3: std::tuple (<tuple>)
std::tuple<std::string, int, double> get_user();
std::get<0>(t), std::get<1>(t)
Structured bindings (C++17) ✅
Unpack any of the above into named variables:
auto [lo, hi] = min_max(v);
auto [name, age, balance] = get_user();
auto [min, max, avg] = compute(data); // works with structs too
std::optional<T> (C++17, <optional>) — "a value or nothing"
std::optional<int> find_index(const std::vector<int>& v, int target) {
for (...) if (...) return i;
return std::nullopt; // nothing found
}
if (auto idx = find_index(v, 42)) { // true if it has a value
std::cout << *idx; // or idx.value()
}
int pos = find_index(v, 42).value_or(-1);
Much clearer than magic values like −1 or special flags.
Comparison
| Need | Use |
|---|---|
| Several related named results | struct |
| Quick pair of values | pair + structured bindings |
| Value might be missing | std::optional |
| Value OR an error reason | std::expected (C++23, Unit 15) |
| Modify caller's variables | reference parameters |
Returning large objects is cheap
Compilers use RVO (return value optimization) and move semantics: returning a big vector or struct by value usually doesn't copy it.
Example
#include <iostream>
#include <optional>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
struct Stats { double min, max, average; };
Stats compute_stats(const std::vector<double>& v) {
Stats s{ v[0], v[0], 0 };
for (double x : v) {
if (x < s.min) s.min = x;
if (x > s.max) s.max = x;
s.average += x;
}
s.average /= static_cast<double>(v.size());
return s;
}
std::pair<int, int> divide(int a, int b) { return { a / b, a % b }; }
std::tuple<std::string, int, double> get_user() { return { "Asha", 24, 12500.50 }; }
std::optional<std::size_t> find_category(const std::vector<std::string>& cats, const std::string& name) {
for (std::size_t i = 0; i < cats.size(); ++i)
if (cats[i] == name) return i;
return std::nullopt;
}
int main() {
auto [lo, hi, avg] = compute_stats({ 850, 1500, 300, 2200 }); // structured bindings on a struct
std::cout << "min " << lo << ", max " << hi << ", avg " << avg << '\n';
auto [quotient, remainder] = divide(47, 5);
std::cout << "47 / 5 = " << quotient << " remainder " << remainder << '\n';
auto [name, age, balance] = get_user();
std::cout << name << " (" << age << ") has Rs. " << balance << '\n';
std::vector<std::string> cats = { "Food", "Rent", "Travel" };
if (auto idx = find_category(cats, "Rent"))
std::cout << "Rent found at index " << *idx << '\n';
auto missing = find_category(cats, "Gym");
std::cout << "Gym found? " << std::boolalpha << missing.has_value()
<< ", value_or(99): " << missing.value_or(99) << '\n';
return 0;
}
/* Output:
min 300, max 2200, avg 1212.5
47 / 5 = 9 remainder 2
Asha (24) has Rs. 12500.5
Rent found at index 1
Gym found? false, value_or(99): 99
*/