473,408 Members | 2,832 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,408 software developers and data experts.

Function with more than one return value

Dear All
I want to make a function in C++ language where there will more than one return value by reference simultaneously.
How can I do this.
Help needed. Thanks in advance.
regards.
Tapasi
Nov 7 '07 #1
4 3486
sicarie
4,677 Expert Mod 4TB
If the objects are of the same type, return them in an array.
Nov 7 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
If the objects are of the same type, return them in an array.
Functions cannot return an array.

You cannot return more than one type.

Actuallty, the only time you ever want top return a value is when you want your function to be usable as an RVAL. That is, it will be used on the right-side of an assignment operator:
Expand|Select|Wrap|Line Numbers
  1. int  result =  MyFunction();
  2.  
Here MyFunction() needs to return an int.

In all other cases you do not use the return type.

For several types to be returned simulataneously, use output arguments. If the function has to create the object, then pass in the address of a pointer so the function can out the address of the newly created onject inside it. If the object already exists and the functions need to update it, then pass the object by reference.

This allows the return type to be a completion code that that caller can test to see what happened.
Nov 7 '07 #3
sicarie
4,677 Expert Mod 4TB
Functions cannot return an array.

You cannot return more than one type.
Dang, getting C++ and Java confused again. Sorry about that.
Nov 7 '07 #4
RRick
463 Expert 256MB
I want to make a function in C++ language where there will more than one return value by reference simultaneously.

The only way to return more than one value is to group the values together in a class or structure.

You can return a class or structure by reference, but you have to guarantee that the object will still exist after the function is called.

Usually, a return by reference means the object or function that returns the reference controls the deletion of the object. If you want to pass resource control of the object to the calling function, you usually pass a pointer.
Nov 7 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
7
by: Felix Kater | last post by:
Hi, when I need to execute a general clean-up procedure (inside of a function) just before the function returns -- how do I do that when there are several returns spread over the whole function?...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.