-5
int a;int b;int c;

If i want to return a, b, and c. how I should do that? I did the following way. but it gives me error.

return a, b, c;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
tani
  • 17
  • 8

5 Answers5

3

If i want to return a, b, and c. how I should do that?

Option 1

Define a struct and return an object of the struct.

struct MyStruct
{
   int a;
   int b;
   int c;
};

Now you can use:

return MyStruct{10, 20, 30};

Option 2

Use std::tuple.

using MyData = std::tuple<int, int, int>;

and then

return MyData{10, 20, 30};
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

While C++ doesn't allow you to return multiple values, you can return std::tuples, which are basically the same:

http://en.cppreference.com/w/cpp/utility/tuple

Or, if you want to be a bit more explicit, you can return a struct.

Alternatively, you can take an "output argument" by reference and overwrite it:

void returnTwoThings(int& out1, int&out2) {
    out1 = 42;
    out2 = 16;
}

int a = 0;
int b = 0;
returnTwoThings(a, b);

// Prints "a: 42 b: 16"
std::cerr << "a: " << a << " b: " << b << std::endl;
Chris Kitching
  • 2,559
  • 23
  • 37
  • how to write this in c++ printf("The value is: (x1, y1) = (%d, %d), (x2, y2) = (%d, %d), (x3, y3) = (%d, %d)\n", x1, y1, x2, y2, x3, y3); ? – tani Mar 04 '16 at 05:04
  • What's that got to do with your original question, what language is that to start with, and what are you even trying to _do_ anyway? – Chris Kitching Mar 04 '16 at 05:05
0

You can either return a struct containing all three integers

struct IntTrio
{
    int a;
    int b;
    int c;
}

IntTrio foo()
{
    int a, b, c;
    // ...
    return {a, b, c};
}

You can also return an std::tuple<int, int, int>, though the brace syntax for return won't work in C++14, you will need std::make_tuple

Or you can simply return your favorite fixed-sized container, such as std::array<int, 3> containing all three values.

KABoissonneault
  • 2,359
  • 18
  • 17
  • how to write this in c++ printf("The value is: (x1, y1) = (%d, %d), (x2, y2) = (%d, %d), (x3, y3) = (%d, %d)\n", x1, y1, x2, y2, x3, y3); – tani Mar 04 '16 at 05:06
  • @tani That seems like a completely different question. Ask a new question officially on the site – KABoissonneault Mar 04 '16 at 05:07
0

In C++ or C, you can return only one value/variable. If you are returning an integer it should be only one 'int'. If you want to return multiple values, put those values in a 'struct'(structure) and change the return type of the function and proceed with returning the 'struct'.

struct StructureName
{  
   int a;
   int b;
   int c;
};

struct StructureName functionName(input_arguments)
{   
struct StructureName var;
////your operations
var.a=10;
var.b=20;
var.c=30;
return var;
}
Ice Fire
  • 1
  • 2
0

Technically in C++ you can only return a single value from a function. To return more values you need to wrap them in a structure. Luckily the standard provides a type for this:

You can use std::tuple to return multiple values.

 std::tuple<int,int,int> getStuff()
 {
      return {2, 3, 4};
 }

 int main()
 {
     int x,y,z;

     // Don't need to use a tuple object to catch the result.
     // You can use std::tie to extract the values directly.
     std::tie(x, y, z) = getStuff();



     // If you don't want to use one of the values use `std::ignore`
     std::tie(x, std::ignore, z) = getStuff();


     // Or you can store them in a tuple.
     auto tup = getStuff();

 }
Martin York
  • 257,169
  • 86
  • 333
  • 562