struct TWO_NUMBERS
{
int a;
int b;
};
TWO_NUMBERS blah(int a, int b)
{
TWO_NUMBERS TwoNumbers;
TwoNumbers.a = a;
TwoNumbers.b = b;
return TwoNumbers;
}
I believe c++11 allows you the ability to return tuples. So you should check that out
1 2 3 4 5 6 7
#include <tuple>
std::tuple<int, int> blah(int a, int b)
{
// some code
return std::make_tuple(a, b);
}
Of course you can decide to go with std::pair as suggested, but if you want to return more than 2 items, c++11 has variadic templates, tuples, initializer_list and of course tuple unpacking. So you can read up on those if you are interested
Of course make sure you understand passing variables by reference before you start returning structs from every function, just to make sure you aren't asking because of that.
Well i haven't learnt structs/pair/tuple.
This clears up a lot because i always tried to do it and failed and it made me think i didn't know how to use functions.
Finally, i can move on in my tutorial . . . i think.
Look up "Passing by value vs Passing by reference" and you will see many useful topics explaining why it is important. Basically you pass variables by member address so the original variable gets updated inside the function and a copy does not have to be made/you don't have to try and return things. In this case it would be int blah(int &a, int &b)
I wouldn't say either one is "better" than the other, it depends on what you are trying to accomplish.
As LB said if you are creating a completely new variable based on the parameters it is more likely you will want to return it from that function. If you just want to modify some of the values that you passed in to the function you would pass by reference instead of returning a value (In general).
@LB I know specific output variables aren't the best idea I just wanted to make sure OP knew passing by reference and wasn't trying to learn returning multiple values for the wrong reasons.