473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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,

Aug 24 '07 #1
6 3736
sr*********@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,intf()
{
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, intf ()
{
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
Aug 24 '07 #2
sr*********@gmail.com 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
Aug 24 '07 #3
On 2007-08-24 14:33, Joe Greer wrote:
sr*********@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,intf()
{
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, intf ()
{
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
Aug 24 '07 #4
sr*********@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++. 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/product...ntists/?usenet
Aug 24 '07 #5
On Aug 25, 4:28 am, Jon Harrop <j...@ffconsultancy.comwrote:
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.

Aug 25 '07 #6
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,doublefoo() {
return std::make_pair(4,4.0);
}

--
Obnoxious User
Aug 25 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
3
by: Varun | last post by:
Hi There, I have a form("myRequest.asp") and the values from it are retrieved into the page ("output_Print.asp") on which I have two buttons('Save As Complete' and 'Save As Incomplete'). When the...
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
1
by: Guha | last post by:
I have a problem with returning a 2D array using a function which is called in main(). The piece of the code is given below. This is a test code only. #include"stdio.h" #include"alloc.h" ...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
14
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
1
by: vijay.gandhi | last post by:
Hello, I have created a function in C++/CLI which was exported as a .DLL to be used in VB .NET. I have been having some problems (I think it has to do with the right syntax) with parameter...
11
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.