Function returning two values



 DEVELOP > c-Plus-Plus > Function returning two values

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 24 Aug 2007 07:07:36 AM
Object: Function returning two values
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,
.

User: "Joe Greer"

Title: Re: Function returning two values 24 Aug 2007 07:33:30 AM
wrote in news:1187957256.897960.56060
@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
.
User: "=?ISO-8859-1?Q?Erik_Wikstr=F6m?="

Title: Re: Function returning two values 24 Aug 2007 07:53:57 AM
On 2007-08-24 14:33, Joe Greer wrote:

srini4vasan@gmail.com wrote in news:1187957256.897960.56060
@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.

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);
--
Erik Wikström
.


User: "Victor Bazarov"

Title: Re: Function returning two values 24 Aug 2007 07:35:27 AM
wrote:

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[s]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.

User: "Jon Harrop"

Title: Re: Function returning two values 24 Aug 2007 06:28:42 PM
wrote:

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++.
--
Dr Jon D Harrop, Flying Frog Consultancy
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?usenet
.
User: ""

Title: Re: Function returning two values 25 Aug 2007 02:34:08 AM
On Aug 25, 4:28 am, Jon Harrop <j...@ffconsultancy.com> wrote:

srini4va...@gmail.com wrote:

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++.

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.
.


User: "Obnoxious User"

Title: Re: Function returning two values 24 Aug 2007 07:08:51 AM
On Fri, 24 Aug 2007 05:07:36 -0700, srini4vasan wrote:

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);
}
--
Obnoxious User
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER