28

Possible Duplicate:
can a function return more than one value?

I want to return 3 variables from a c++ function but as one function can have only one running return value how I can return 3 values. tried using return(5,4); but this is an syntax error.

Community
  • 1
  • 1
Sudhir Chopra
  • 281
  • 1
  • 3
  • 3

11 Answers11

50

A C++ function can return only one value. However, you can return multiple values by wrapping them in a class or struct.

struct Foo
{
     int value1;
     int value2;
};

Foo SomeFunction()
{
    Foo result = { 5, 4 };
    return result;
}

Or you could use std::tuple, if that is available with your compiler.

#include <tuple>

std::tuple<int, int, int> SomeFunction()
{
    return std::make_tuple(5, 4, 3);
}

If you don't know in advance how many values you're going to return, a std::vector or a similar container is your best bet.

You can also return multiple values by having pointer or reference arguments to your function and modifying them in your function, but I think returning a compound type is generally speaking a "cleaner" approach.

Sven
  • 21,903
  • 4
  • 56
  • 63
  • 9
    +1 for std::tuple. :) – Macke Jun 18 '11 at 08:20
  • 1
    If you want to accept multiple values from a function, a convenient way of doing this is to use `std::tie` http://stackoverflow.com/a/2573822/502144 – fdermishin May 12 '14 at 08:40
  • 1
    @SteeveDroz, How will you print the tuple once it's written in your `main()` function?`auto tup= swap(a,b); std::cout << tup << std::endl;`, I get `error: cannot bind ‘std::ostream {aka std::basic_ostream}’ lvalue to ‘std::basic_ostream&&’` error. Any suggestions on it – Anu Jan 14 '19 at 22:15
  • Yes, I suggest you ask @Sven instead of me. I have no idea and I didn't give that answer. – SteeveDroz Jan 15 '19 at 07:53
7

You can only return one value in C++. If you need to return more information, return a structure (or a container like a std::vector for example), or pass in some variables as non-const references (or pointers) and change those.

Mat
  • 202,337
  • 40
  • 393
  • 406
7

tried using return(5,4); but this is an syntax error.

That is not a syntax error. (5,4) is a valid expression and here , is an operator. So the expression (5,4) evaluates to the rightmost operand, which is 4. Hence it will return 4.


Now coming to your problem: define a struct if any existing one doesn't help you, and return an object of struct instead, as:

struct values
{
   int i;
   int j;
   char *other;
};

values f()
{
  values v = {/*....*/};
   //...
   return v;
}

And if type of all values is same, then you can use std::vector as:

#include <vector>  //must include it!

std::vector<int> f()
{
   std::vector<int> v;
   v.push_back(5);
   v.push_back(4);
   //so on
   //...
   return v;
}

There are other containers as well, viz. std::map, std::list, std::stack, etc. Use which suits you the best. There is also std::pair which holds only two values, as the name implies.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
4

Or you could use std::pair for two results and look at boost::tuple for more than two. It can even handle different types in the same tuple.

luvieere
  • 37,065
  • 18
  • 127
  • 179
4

There are several ways to achieve that.

  1. If you have objects of the same type, std::array would be a good return type for that.
    If you have varying numbers, use std::vector instead.

  2. If you have (a fixed number of) different types, use std::tuple:

    std::tuple <int, long, double> f()
    {
      return std::tuple <int, long, double>( 42, 4711l, 3.141 );
    }
    

I know no way to return a varying number of non-polymorphic objects of different types built into the language or standard library.

sbi
  • 219,715
  • 46
  • 258
  • 445
1

No, but there are two ways to do that:

1) Give the pointers to the values to "return" as parameters, you change the value of it in the function.

2) You may create a struct that contains all these values and return the struct. Use this only if it is really necessary, though, the fist solution is much better.

SteeveDroz
  • 6,006
  • 6
  • 33
  • 65
1

Most of the answers here are correct that you should return a struct. There is one more way if you are interested, and that is by sending reference of the variables that you want to return.

For example:

#include<iostream>
#include<cstdio>
using namespace std;
bool f(int a,int b,int& q,int& r)
{
    if(b==0)return 0;
    q=a/b;
    r=a%b;
    return 1;
}

int main()
{
    int a=64,b=7,q,r;
    bool pos=f(a,b,q,r);
    if(pos)
    {
        printf("Quotient = %d Remainder = %d\n",q,r);
    }
    else
    {
        printf("Divison by zero not possible!\n");
    }
    return 0;
}
0

return (a, b, c, ... , n) The statements a, b, c are separated with comma operators and the expression evaluates to the rightmost operand n . So the syntax is correct, but does not return multiple values.

To return multiple values:

  • Put the variables you want to return in a structure and return it.
  • Or make an array of multiple variables (dynamically allocated) and return the base address
phoxis
  • 60,131
  • 14
  • 81
  • 117
0

It is possible to return more than one values from a function you have to use pointer for that case. Also this is possible by using the pointer arguments pointer arguments keep their value' Detailed usage of pointer for returning more than one value is at http://www.techyv.com/questions/how-do-i-return-multiple-variables-c-function

0

Return a structure.

struct a
{
   int x;
   int y;
   int z;
};

b()
{
    a.x = 2;
    a.y = 4;
    a.z = 8;
    return a;
}
jdl
  • 6,151
  • 19
  • 83
  • 132
-1

You can't. You can, however, do tricks to fake it. For example, make your function return a pointer to int, and returning the pointer to an array of ints.

Jasmijn
  • 9,370
  • 2
  • 29
  • 43