Return multiple values from functions in C++
This post will discuss how to return multiple values from functions in C++.
We know that the syntax of a function in C++ doesn’t permit returning of multiple values. But programmers often need to return multiple values from the functions. Luckily, there are many alternatives in C++ to return multiple values.
1. Using reference parameters in C++
We have seen that we can use pointers in C to return more than one value from a function in the previous post. In C++, we have reference variables to achieve the same.
The idea is to declare the function parameters as references rather than as pointers. Now any changes made to the reference are reflected in the original variable itself. The following C++ program demonstrates it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> // Function to return multiple values using references void initialize(int &a, int &b, char &c) { a = 10; b = 20; c = 'A'; } // Return multiple values from functions in C++ int main() { int a, b; char c; initialize(a, b, c); std::cout << "a = " << a << ", b = " << b << ", c = " << c; return 0; } |
Output:
a = 10, b = 20, c = A
2. Using std::pair
in C++
If we’re required to return only two values from the function, the recommended way is to use the std::pair
. The following C++ program demonstrates it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> // Function to return two values using `std::pair` std::pair<std::string, int> initialize() { return std::make_pair("A", 65); } // Return two values from a function in C++ int main() { std::pair<std::string, int> p = initialize(); std::string s = p.first; int i = p.second; std::cout << "The returned string is " << s << " and integer is " << i; return 0; } |
Output:
The returned string is A and integer is 65
3. Using std::tuple
in C++11
std::pair
works fine for returning two values, but it’s not flexible if we need to add more values later. From C++11 onward, we can use std::tuple
that can return any number of values. The following C++ program demonstrates it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <tuple> // Function to return multiple values using references std::tuple<int, int, char, double> initialize() { return std::make_tuple(10, 20, 'A', 1.2f); } // Return multiple values from functions in C++ int main() { int a, b; char c; double d; std::tie(a, b, c, d) = initialize(); std::cout << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d; return 0; } |
Output:
a = 10, b = 20, c = A, d = 1.2
We have assigned the returned values to corresponding variables using std::tie
. We can also use std::get
to access tuple members.
Before C++11, the recommended alternative to std::tuple
is to use Boost Tuple Library.
4. Using std::map
or std::unordered_map
(in C++11)
We can also use a map to return multiple values from a function, but this will work only if all values have the same data type. The following C++ program demonstrates it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <unordered_map> using namespace std; // Function to return multiple values using a map unordered_map<string, int> initialize() { unordered_map<string, int> mapping; mapping["a"] = 10; mapping["b"] = 20; return mapping; } // Return multiple values from functions in C++ int main() { unordered_map<string, int> mapping = initialize(); int a = mapping["a"]; int b = mapping["b"]; cout << "a = " << a << ", b = " << b; return 0; } |
Output:
a = 10, b = 20
Starting from C++17, we can use Structured bindings, which offers an easier syntax to initialize values received from a pair or tuple.
That’s all about returning multiple values from functions in C++.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)