I have a function, and I need to get two values from there. Function can return only one value. I read somewhere that I can use pointer to get second value, when I'm trying to do this I'm getting error so it looks pointer value can't go outside of visibility zone. So that means I have only one value. How I can get second value from function using pointer?
-
2Reading something "somewhere" is not a very productive way to learn C++. The correct way is with a good C++ textbook, which will explain how to use `std::tuple`, and give examples of using it, to do just this. – Sam Varshavchik Jun 17 '20 at 13:02
-
`std::tuple
GetValues() { return {10, 20}; }` and `auto [a, b] = GetValues();` – Eljay Jun 17 '20 at 13:03 -
References are usually preferred instead of pointers (depending on use-case). Or you can use some kind of structure or class to group related values more tightly. Or use some kind of standard structure (like `std::tuple` as already mentioned) for a more loose grouping. – Some programmer dude Jun 17 '20 at 13:04
-
@Evg and others: No, no, no. Use a class with named members (in most cases). – eerorika Jun 17 '20 at 13:04
-
1related/dupe: https://stackoverflow.com/questions/321068/returning-multiple-values-from-a-c-function – NathanOliver Jun 17 '20 at 13:06
-
Tried ways you suggested. All good. Thank you. – Fedor Petrenko Jun 18 '20 at 14:38
3 Answers
An instance of a certain type is "one value". Consider this class:
struct foo {
int bar;
double moo;
};
Now I can write a function that returns one value of that type:
foo asdf() {
foo result;
result.bar = 42;
result.moo = 3.14159;
return result;
}
PS: Don't use pointers. It is possible to use so-called out-parameters, but if you do you should use a reference instead of a pointer:
void asdf2(int& bar,int& moo) {
bar = 42;
moo = 3.14159;
}
Call like this:
int x = 0;
double y = 0.;
asdf2(x,y);
However, out parameters are not nice, because in foo f = asdf();
it is more obvious what is returned from the function compared to asdf2(x,y)
.

- 109,796
- 11
- 89
- 185
The idea that you read somewhere is that the code that calls the function, creates the object, passes a pointer to the object as an argument to the function, and the function assigns to the object by indirecting through the pointer.
However, if you were to do this, you should prefer to use a reference instead of a pointer, so that you don't have to deal with the possibility that someone passes null to the function.
In most cases, the best approach is to return an instance of a class instead. Even though you can only return one object, a class can have multiple sub objects. For example, if you wanted to return two coordinates into an euclidean space, you might do:
struct Point {
double x;
double y;
};
Point example()
{
// until C++20
return {
42.,
1337.,
};
// since C++20
return {
.x = 42.,
.y = 1337.,
};
}
Here, we returned two values within a single object.

- 232,697
- 12
- 197
- 326
There are at least three approaches.
The first one as you already mentioned is to return a value using a parameter of a pointer type.
For example
#include <iostream>
int minmax( int x, int y, int *max )
{
int min = x;
*max = y;
if ( y < x )
{
min = y;
*max = x;
}
return min;
}
int main()
{
int min, max;
min = minmax( 20, 10, &max );
std::cout << "min = " << min << ", max = " << max << '\n';
}
The program output is
min = 10, max = 20
Here in the program the variable max
is passed by reference in the terms of C.
The second approach is to pass by reference in the terms of C++. For example
#include <iostream>
int minmax( int x, int y, int &max )
{
int min = x;
max = y;
if ( y < x )
{
min = y;
max = x;
}
return min;
}
int main()
{
int min, max;
min = minmax( 20, 10, max );
std::cout << "min = " << min << ", max = " << max << '\n';
}
The program output is the same as shown above.
And you can indeed return one object but of a compound type that contains other sub-objects. For example
#include <iostream>
#include <utility>
std::pair<int, int> minmax( int x, int y )
{
return y < x ? std::pair<int, int>( y, x ) : std::pair<int, int>( x, y );
}
int main()
{
auto p = minmax( 20, 10 );
std::cout << "min = " << p.first << ", max = " << p.second << '\n';
}
The program output also is the same as shown for the first demonstrative program.
Instead of the standard class template std::pair that has two data members you can use the class template std::tuple
if you want to return more than two objects. Or you can write your own compound type (template or not template) as a structure or class.
For example
#include <iostream>
#include <tuple>
std::tuple<int, int, int> get_sorted( int x, int y, int z )
{
if ( not ( y < x ) && not ( z < x ) )
{
return not ( z < y ) ? std::make_tuple( x, y, z )
: std::make_tuple( x, z, y );
}
else if ( not ( z < y ) )
{
return not ( z < x ) ? std::make_tuple( y, x, z )
: std::make_tuple( y, z, x );
}
else
{
return not ( y < x ) ? std::make_tuple( z, x, y )
: std::make_tuple( z, y, x );
}
}
int main()
{
auto t = get_sorted( 15, 10, 20 );
std::cout << "( " << std::get<0>( t )
<< ", " << std::get<1>( t )
<< ", " << std::get<2>( t )
<< " )\n";
}
The program output is
( 10, 15, 20 )

- 301,070
- 26
- 186
- 335