### How to return multiple data from a function

Hello fellows,
In a function , I do some mathematical and obtain a vector , a double value and one 2D matrix. I need to use all these three outputs later in another function.

void functon1()
{
.... some mathematical operations
I obtain V1=[ 3 6 9]
I obtain M1=[[ 0 1 0]
[ 4 7 2]
[ 8 3 7]]
and I calculate a double called sum.
}

How can I return these three outputs? and how can I use them in another function later ? For example later I will call function1 and want to use sum value, or 2D array (M1) only? Is it possible? If yes, can you give me an example, I could not find what I need on the internet. Thank you so much

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include void obtain(std::vector&, double&, std::vector>&); void use(std::vector&, double&, std::vector>&); int main() { std::vector v; double d; std::vector> v2d; obtain(v, d, v2d); use(v, d, v2d); } void obtain(std::vector& v, double& d, std::vector>& v2d) { v = { 1, 2, 3 }; d = 1.23; v2d = { { 1, 3, 5 }, { 2, 4, 6 } }; } void use(std::vector& v, double& d, std::vector>& v2d) { for ( auto& itr : v ) { std::cout << itr << ' '; } std::cout << "\n\n"; std::cout << d << "\n\n"; for ( auto& row : v2d ) { for ( auto& col : row ) { std::cout << col << ' '; } std::cout << '\n'; } }``````
 ```1 2 3 1.23 1 3 5 2 4 6```
you can also return a tuple or similar construct.
Using a tuple (I was crufting the example when jonnin replied):
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include #include std::tuple, double, std::vector>> obtain(); void use(std::tuple, double, std::vector>>&); int main() { std::tuple, double, std::vector>> c = obtain(); use(c); } std::tuple, double, std::vector>> obtain() { std::vector v { 1, 2, 3 }; double d { 1.23 }; std::vector> v2d { { 1, 3, 5 }, { 2, 4, 6 } }; return std::make_tuple(v, d, v2d); } void use(std::tuple, double, std::vector>>& t) { for ( auto& itr : std::get<0>(t) ) { std::cout << itr << ' '; } std::cout << "\n\n"; std::cout << std::get<1>(t) << "\n\n"; for ( auto& row : std::get<2>(t) ) { for ( auto& col : row ) { std::cout << col << ' '; } std::cout << '\n'; } }``````
Using auto liberally can reduce the code's verbosity a bit, with an arguable somewhat loss of readability:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include #include auto obtain() { std::vector v { 1, 2, 3 }; double d { 1.23 }; std::vector> v2d { { 1, 3, 5 }, { 2, 4, 6 } }; return std::make_tuple(v, d, v2d); } void use(auto& t) { // C++17 structured binding unpacks the std::tuple auto [v, d, v2d] = t; for ( auto& itr : v ) { std::cout << itr << ' '; } std::cout << "\n\n"; std::cout << d << "\n\n"; for ( auto& row : v2d ) { for ( auto& col : row ) { std::cout << col << ' '; } std::cout << '\n'; } } int main() { auto c { obtain() }; use(c); }``````
Last edited on
Thank you so much for all these examples, @George P and @jonnin. I learnt a lot about how to return tuples from a function.
Another way to return multiple values would be use a struct.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include using Array = std::vector; using Matrix = std::vector; struct Ret { Array a; double d {}; Matrix m; }; auto obtain() { const Array v { 1, 2, 3 }; const double d { 1.23 }; const Matrix v2d { { 1, 3, 5 }, { 2, 4, 6 } }; return Ret {.a = v, .d = d, .m = v2d}; } void use(const Ret& t) { const auto& [v, d, v2d] {t}; for (const auto& itr : v) std::cout << itr << ' '; std::cout << "\n\n" << d << "\n\n"; for (const auto& row : v2d) { for (const auto& col : row) std::cout << col << ' '; std::cout << '\n'; } } int main() { const auto c { obtain() }; use(c); }``````

Last edited on
 ``12345678910111213141516`` ``````#include #include using Array = std::vector; using Matrix = std::vector; struct Ret { Array a; double d {}; Matrix m; }; int main() { std::tuple atu; Ret ast; }``````

The atu and ast are both essentially structs.
The type `Ret` we define explicitly, while the other type is generated by the compiler from a template
based on instantiation `std::tuple<Array, double, Matrix>`.
Topic archived. No new replies allowed.