Function returning two values

S

srini4vasan

Hi,
Is that possible to return two values from a functions at a time.

ie.,Can a function return two values at a time.

if()
{
return
}
else
{
return
}
} // Not like this.

Anything apart from this.

Thanks,
 
O

Obnoxious User

Hi,
Is that possible to return two values from a functions at a time.

ie.,Can a function return two values at a time.

if()
{
return
}
else
{
return
}
} // Not like this.

Anything apart from this.

std::pair<int,double> foo() {
return std::make_pair(4,4.0);
}
 
J

Joe Greer

(e-mail address removed) wrote in @e9g2000prf.googlegroups.com:
Hi,
Is that possible to return two values from a functions at a time.

ie.,Can a function return two values at a time.

if()
{
return
}
else
{
return
}
} // Not like this.

Anything apart from this.

Thanks,

Sure, there are lot's of ways. What do you have in mind? One way is to
define a structure:

struct ReturnValue {
int value1;
int value2;
};

ReturnValue f()
{
ReturnValue f;
f.value1 = 5;
f.value2 = 6;
return f;
}

A more general approach might be to use std::pair<>

std::pair<int,int> f()
{
return std::make_pair(5,6);
}

If you have access to TR1 or boost, you can use tuples. Tuples are cool
because you can also use ties


boost::tuple<int, int> f ()
{
return boost::make_tuple(5,6);
}

The cool thing here is that you can also do:

int a,b;

boost::tie(a,b) = f();

Now 'a' has the value 5 and b the value 6. This is also available int
TR1, I believe.

As you can see, there are lots of choices. Which you choose depends
upon what you are doing and what you have available.

joe
 
V

Victor Bazarov

Hi,
Is that possible to return two values from a functions at a time.

If this is a question, then the direct and short answer is "no".
Of course, there are work-arounds: you can have in-out arguments
(object passed by reference and changed inside the function, or
pointers that allow change the object they point to), you can have
the return value a struct that combines the values you want to
return from your function [using the 'return' statement], you
can throw the same structure, you can fill the global variable
with the values to be "returned"...
ie.,Can a function return two values at a time.

if()
{
return
}
else
{
return
}
} // Not like this.

Not like *what*? (a) there is no function above, (b) even if we
assume that it's the code wrapped in a function, the 'return'
statements don't have any values the function would return.
Anything apart from this.

Anything apart from WHAT?

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

(e-mail address removed) wrote in @e9g2000prf.googlegroups.com:


Sure, there are lot's of ways. What do you have in mind? One way is to
define a structure:

struct ReturnValue {
int value1;
int value2;
};

ReturnValue f()
{
ReturnValue f;
f.value1 = 5;
f.value2 = 6;
return f;
}

A more general approach might be to use std::pair<>

std::pair<int,int> f()
{
return std::make_pair(5,6);
}

If you have access to TR1 or boost, you can use tuples. Tuples are cool
because you can also use ties


boost::tuple<int, int> f ()
{
return boost::make_tuple(5,6);
}

The cool thing here is that you can also do:

int a,b;

boost::tie(a,b) = f();

Now 'a' has the value 5 and b the value 6. This is also available int
TR1, I believe.

As you can see, there are lots of choices. Which you choose depends
upon what you are doing and what you have available.

And there's also the usage of reference/pointer parameters:

int func(int a, int b, int& ret)
{
// Do something, set r2 to the second return value
// and return the first return value as normal
}

int r1, r2;
r1 = func(1, 2, r2);
 
J

Jon Harrop

Is that possible to return two values from a functions at a time.

Absolutely, yes.

For example, the following OCaml function returns x and x^2:

let f x = x, x*x

This may be written in C++ as:

int f(int x) {
return std::pair<int, int>(x, x*x);
}

This style of programming is very fast in modern languages like OCaml but
incurs a significant overhead in languages like C++. Consequently, a common
alternative is the low-level approach of passing in references to
pre-allocated return values that are set by the body of the function call.
This introduces more problems in the absence of a GC, such as vanilla C++.
 
P

phdscholar80

Absolutely, yes.

For example, the following OCaml function returns x and x^2:

let f x = x, x*x

This may be written in C++ as:

int f(int x) {
return std::pair<int, int>(x, x*x);
}

This style of programming is very fast in modern languages like OCaml but
incurs a significant overhead in languages like C++.

I believe Named Return Value (NRV) optimzation takes care of the
problem so the boost::tie and boost::tuple combination ought to work
perfectly. Do correct me if I am wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top