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


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>

void obtain(std::vector<int>&, double&, std::vector<std::vector<int>>&);
void use(std::vector<int>&, double&, std::vector<std::vector<int>>&);

int main()
{
   std::vector<int> v;

   double d;

   std::vector<std::vector<int>> v2d;

   obtain(v, d, v2d);

   use(v, d, v2d);
}

void obtain(std::vector<int>& v, double& d, std::vector<std::vector<int>>& v2d)
{
   v = { 1, 2, 3 };

   d = 1.23;

   v2d = { { 1, 3, 5 }, { 2, 4, 6 } };
}

void use(std::vector<int>& v, double& d, std::vector<std::vector<int>>& 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):
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
#include <tuple>

std::tuple<std::vector<int>, double, std::vector<std::vector<int>>> obtain();
void use(std::tuple<std::vector<int>, double, std::vector<std::vector<int>>>&);

int main()
{
   std::tuple<std::vector<int>, double, std::vector<std::vector<int>>> c = obtain();

   use(c);
}

std::tuple<std::vector<int>, double, std::vector<std::vector<int>>> obtain()
{
   std::vector<int> v { 1, 2, 3 };

   double d { 1.23 };

   std::vector<std::vector<int>> v2d  { { 1, 3, 5 }, { 2, 4, 6 } };

   return std::make_tuple(v, d, v2d);
}

void use(std::tuple<std::vector<int>, double, std::vector<std::vector<int>>>& 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:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
#include <tuple>

auto obtain()
{
   std::vector<int> v { 1, 2, 3 };

   double d { 1.23 };

   std::vector<std::vector<int>> 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.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>

using Array = std::vector<int>;
using Matrix = std::vector<Array>;

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
#include <tuple>

using Array = std::vector<int>;
using Matrix = std::vector<Array>;

struct Ret {
	Array a;
	double d {};
	Matrix m;
};

int main() {
  std::tuple<Array, double, Matrix> 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.