Return multiple value

Pages: 12
hi there
i want to return 2 or more value in function but it's not allowed !
i saw a lot of ways to doing that , but that wasn't what i'm looking for !!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
double calc(double);
int main()
{
        double avg;
	cin >> avg;
	cout << calc(avg) << endl;
	
}
double calc(double avg)
{
	double a, b;
	a = avg;
	b = avg + 2;
	return (a, b);
}
¿so what are you looking for then?
Are you saying that you want this:

return (a, b);

to be a way to return more than one value?

If that's what you want, then you will need to have the C++ language fundamentally changed.

To do so, you need to contact the C++ committee (the people who decide what is correct C++, and change C++ when needed) and submit a proposal.

You can read about how to do that here, at the bottom under the question Q: That’s all about new standard library features. How do I propose a new feature for the C++ core language itself?

https://isocpp.org/std/submit-a-proposal

Good luck. I suspect that it would actually be easier for you to create a whole new programming language to do this.
Last edited on
the problem is that you say that you want to return one double (function prototype) but then decide to return two and expect it to work

1
2
3
std::pair<double, double> foo(){
   return {3.14, 2.79};
}
There are two ways to do this.

1. You can return a struct or class instance.
2. You can pass multiple values by reference and modify them within the function.

I use the second method at work a lot. A function or method will return bool to indicate if it succeeded or failed, and a reference parameter gets populated with the required data if it succeeds.
You can instead do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
double calc(double);
int main()
{
        double avg; double a; double b;
	cin >> avg;
        calc(avg, a, b);
	cout << "a: " << a << "b: " << b << endl;
	
}
void calc(double avg, double &a, double &b)
{
	double a, b;
	a = avg;
	b = avg + 2;
}


Which is what dhayden suggested
Last edited on
A rather typical alternative:

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
#include <iostream>
#include <tuple>
using namespace std;


tuple< double, double > calc(double);

int main()
{
    double avg;
    cin >> avg;
    auto t = calc( avg );
    cout << get<0>(t) << ", " <<  get<1>(t)  << endl; // consider using std::tie perhaps
	
}

tuple< double, double > calc(double avg)
{
	double a, b;
	a = avg;
	b = avg + 2;
	return make_tuple(a, b);
}




This is related to dhayden's option 1.

The objection was, a long time ago, that returning a class by value takes time for the copy. Modern optimizers cause this issue to evaporate. The objection comes from a I time I remember when 64 MBytes of RAM was over $1,000 (that's Mega, not Giga), and the compiler couldn't breathe. We were lucky if it could process templates at all with such little RAM.
Last edited on
Goodness it took a while to mention tuples. (Props to Niccolo!)

C++17 has structured bindings added to the language, so you can simply say:

1
2
auto [a, b] = f( whatever );
cout << a << ", " << b << "\n";

Structured bindings work over structs and classes as well, as long as there is a clear way for the compiler to decipher which member objects are meant. (Otherwise you'll have to define a couple of other functions as part of your class's API to help it along.)
Can we try and remember that we're in the beginner's forum here, please? Rather than writing snide, unhelpful answers effectively mocking the OP for not understanding that you can't literally return 2 values in C++, perhaps we could be a bit kinder and more understanding in helping the OP find a solution to the problem?
^^^^^
If there's something about the code they don't understand, that's good. That's something for them to look up or ask about - It's how I learned a bunch of stuff.
I don't think anyone is mocking (overkill a little, but helpful educational overkill), and I wish you guys had been around to teach me when I was in school. Wasn't even a web yet, though.
Um, one person in this thread chose to respond as if the OP was genuienly asking to have the C++ standard changed, rather than showing any attempt to help them achieve what they actually wanted to achieve. That's unhelpful and, frankly, mean-spirited.
unhelpful and, frankly, mean-spirited


Yup, that's me all over.
i want to return 2 or more value in function


Are you saying that you want this:
return (a, b);


easier for you to create a whole new programming language


Umm ... it's called Python. (And, as @Niccolo picked up on, there it would be returned as a tuple, albeit with far simpler syntax.)

Which, I suspect, is where this (and a number of other beginner threads) are coming from. Universities are increasingly using Python as an initial teaching language, particularly for engineers and scientists, so one may need to be aware of pythonic (but completely non-C++) idioms, like
a, b = c, d

or
if a <= x <= b

or just
return (a, b)


And you would be hard pressed to tell a student that (C++):
for ( const auto &x : S )
is more readable than (Python):
for x in S:
Last edited on
This is C++:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <tuple>

std::tuple<int,char,double> foo() { return { 1, 'a', 2.345 } ; }

int main()
{
    const auto [i,c,d] = foo() ;
    std::cout << i << ' ' << c << ' ' << d << '\n' ;
}

http://coliru.stacked-crooked.com/a/0406e0a3a7348b87
JLBorges wrote:
This is C++:


And this is Python:

1
2
3
4
5
def foo():
    return 1, 'a', 2.345

i, c, d = foo()
print( i, c, d )
Last edited on
And this is JavaScript:

1
2
3
4
5
6
function foo(){ return [ 1, 'a', 2.345 ] ; }

var [a, b, c] = foo();
print( a, b, c ) 

/* or just */ print( foo() )

https://rextester.com/UALQVE25933
So the OP's question definitely wasn't daft!
Umm ... it's called Python.


Very much my whole point.
Pages: 12