Returning Multiple Values from a C++ Function

Return multiple values to a method caller

In C# 7 and above, see this answer.

In previous versions, you can use .NET 4.0+'s Tuple:

For Example:

public Tuple<int, int> GetMultipleValue()
{
return Tuple.Create(1,2);
}

Tuples with two values have Item1 and Item2 as properties.

How do I return multiple values from a function in C?

I don't know what your string is, but I'm going to assume that it manages its own memory.

You have two solutions:

1: Return a struct which contains all the types you need.

struct Tuple {
int a;
string b;
};

struct Tuple getPair() {
Tuple r = { 1, getString() };
return r;
}

void foo() {
struct Tuple t = getPair();
}

2: Use pointers to pass out values.

void getPair(int* a, string* b) {
// Check that these are not pointing to NULL
assert(a);
assert(b);
*a = 1;
*b = getString();
}

void foo() {
int a, b;
getPair(&a, &b);
}

Which one you choose to use depends largely on personal preference as to whatever semantics you like more.

returning multiple values from function c

Functions in C cannot return array types, nor can arrays be the target of an assignment. So code like

int returnedObject[2];
returnedObject = doStuff( a, b );

cannot work. C functions can return pointers to arrays, but I don't think you want to get into that.

C functions cannot return multiple objects, nor is multiple assignment supported.

If your two values are logically attributes of some larger, aggregate type, you can use a struct type to return a single object with multiple attributes, such as

struct result { int x, int y };

struct result doStuff( int a, int b )
{
struct result ret = {a, b}; // initialize struct with inputs
...
case 1:
ret.x = a-2;
break; // y remains unchanged

case 2:
ret.y = b-2;
break; // x remains unchanged
...
return ret;
}

which you would call as

struct result stuff;
stuff = doStuff( a, b );

Alternately, you can pass pointers to your values and update them as necessary:

void doStuff( int *a, int *b )
{
...
case 1:
*a = *a - 2;
break;

case 2:
*b = *b - 2;
break;
}

which you would call as

doStuff( &a, &b );

In this case you don't actually return anything to the caller.

You can pass an array as an argument to the function, like so:

void doStuff( int a, int b, int *result )
{
result[0] = a;
result[1] = b;
...
case 1:
result[0] = a - 2;
break;

case 2:
result[1] = b - 2;
break;
...
}

which you would call as

int results[2];
int a;
int b;
...
doStuff( a, b, results );

Note that when we pass the array expression to the function, its type is changed from int [2] to int *. Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize another array, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array. Array expressions in C lose their "array-ness" in most circumstances1. You can use the subscript operator [] on a pointer expression as you would an array expression (the array index operation a[i] is defined as *(a + i)), but pointers are not arrays and vice versa.


1. Note that I'm talking about the expression, not the object. The array object always stays an array object.

Returning multiple values from a C++ function

For returning two values I use a std::pair (usually typedef'd). You should look at boost::tuple (in C++11 and newer, there's std::tuple) for more than two return results.

With introduction of structured binding in C++ 17, returning std::tuple should probably become accepted standard.

return multiple function values to main in C [duplicate]

return (x, y) will execute the expression x, throw away the result, execute the expression y, and then return the value of the expression y.

To return multiple values, you will either want to use pointer arguments:

void foo(int *a, int *b) {
*a = 3;
*b = 4;
}

int main() {
int x, y;
foo(&x, &y);
assert(x == 3);
assert(y == 4);
}

Or use a struct:

struct Object {
int a;
int b;
};

struct Object foo() {
struct Object obj;
obj.a = 3;
obj.b = 4;
return obj;
}

int main() {
struct Object obj = foo();
int x = foo.a;
int y = foo.b;
}

returning multiple values from a function [duplicate]

Your choices here are to either return a struct with elements of your liking, or make the function to handle the arguments with pointers.

/* method 1 */
struct Bar{
int x;
int y;
};

struct Bar funct();
struct Bar funct(){
struct Bar result;
result.x = 1;
result.y = 2;
return result;
}

/* method 2 */
void funct2(int *x, int *y);
void funct2(int *x, int *y){
/* dereferencing and setting */
*x = 1;
*y = 2;
}

int main(int argc, char* argv[]) {
struct Bar dunno = funct();
int x,y;
funct2(&x, &y);

// dunno.x == x
// dunno.y == y
return 0;
}


Related Topics



Leave a reply



Submit