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

How to return 2 or more values?

Hi guys
I just want to know how I can return more than one value in a function.
For example I send tow integers to my function and then I do some operation on them then I want to send them back again. It means in my last line of my function I have:

return a, b;

Is it possible? Why its not working? And if its possible how I can get them back in my main function?
Aug 25 '07 #1
7 3173
Hi guys
I just want to know how I can return more than one value in a function.
For example I send tow integers to my function and then I do some operation on them then I want to send them back again. It means in my last line of my function I have:

return a, b;

Is it possible????
Why its not working????? And if its possible how I can get them back in my main function????

Hi,

Function with "pass by reference" is used to return more than one value.
Aug 25 '07 #2
soudam
2
Hi,

Function with "pass by reference" is used to return more than one value.
you can use a pointer (to what type of data you are returning) to return more than one data
just take an array & return the base address.................
thats gonna be work for sure........
Aug 25 '07 #3
you can use a pointer (to what type of data you are returning) to return more than one data
just take an array & return the base address.................
thats gonna be work for sure........
it will work for sure but there is one issue here. if the array is declared locally in the function and we try to return the base address, and if that base address is used to read those values it might throw some error or show some garbage value. this is because. consider the following code:
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  
  4. int a,b; 
  5. int *p;
  6. p=processdata(a,b);   // function taking two arguments and returning address
  7. cout<<"first element= "<<*p <<" second element "<<*(p+1);
  8.     // p is pointing to first element
  9.    // p+1 pointing to second element
  10.  
  11. }
  12.  
  13. int* processdata(a,b)     // function returning an address
  14. {
  15.  int c[2];  // locally declaerd array
  16.  c[0]=a;   
  17.  c[1]=b;
  18.  c[0]+=10;
  19.  c[1]+=20;   // some processing on data
  20. return c;   // returns the base address
  21. }
  22.  
  23.  
the code looks fine but the problem is that the address returned by processdata() function might not belong to the program. because the local variables of function are created on stack as soon as the { block is enetered and the variables are destroyed if } is encountered.
so, when the control enters { array c is created on some address belonging to stack after } is reached the array c is destroyed alongwith its values and stack is released such that it can be used by some other functions. and if the same memory loactio used by c is overwritten by some other function cout<<*p will show some entirely diff. value. it all depends upon whether the stack is used by some other functions or not.
i hope the concept is clear. any suggestions are welcome.
Aug 26 '07 #4
gsi
51
HI,
A function can never return more than one value, but u have the liberty to return an aggregate object (an array, structure). Since arrays are implicitly passed in and returned as references (pointers) in c/c++ you will land up in a dangling reference on scope exit if the array was local inside the returning function. The simplest but may not be the safest one , is to populate the return values in a structure and return it(Assuming return values does not contain references(pointers)).

eg,

Expand|Select|Wrap|Line Numbers
  1.  
  2. struct A{
  3. int a,b;//depends on ur return values desired.
  4. };
  5.  
  6. A foo(){
  7. // store the return values in a local struct A object and return
  8. }
  9.  
  10. int main(){
  11. A returnvalues = foo();
  12. }
  13.  
  14.  
  15. Another way is to create the structue (to store in return values) in caller and pass it as a reference to the callee (Efficient in case of ur example).
  16.  
  17.  
  18. Thanks,
  19. gsi.
  20.  
Aug 26 '07 #5
Hi,

Function with "pass by reference" is used to return more than one value.
Yes, this is simplest method to solve the problem, to expand on this a little:

Rather than passing the values into the function with a copy, pass them by reference. Pass by reference can be thought of as an alias, or another name for the variables.

Expand|Select|Wrap|Line Numbers
  1. void func_name(int &a, int &b)
  2. {
  3.    your function;
  4. }
  5.  
  6. int main ()
  7. {
  8.    int x;
  9.    int y;
  10.  
  11.    func_name(x, y);
  12.  
  13. }
  14.  
the x and y integers are passed dierectly to func_name, and they are processed/changed by the function. After the call to func_name, the main program sees their new values.

Chris
Aug 26 '07 #6
gsi
51
Hi,
Rather than passing the values into the function with a copy, pass them by reference.
Agreed, but I suppose that is better only in the case when the arguments to the function are the return values (eg, passing references to a swap function).

Thanks,
gsi.
Aug 26 '07 #7
Thanks a lot guys

Sam
Aug 27 '07 #8

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
2
by: Chris | last post by:
I think I already know that the answer is that this can't be done, but I'll ask anyways. Suppose you want to use an RDBMS to store messages for a threaded message forum like usenet and then...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
1
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event,...
5
by: Edward Diener | last post by:
I am gathering from the documentation that return values from __events are not illegal but are frowned upon in .NET. If this is the case, does one pass back values from an event handler via...
43
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
9
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
4
by: Jonathan | last post by:
I have a SQL stored procedure for adding a new record in a transactions table. It also has two return values: CounterID and IDKey. I want to create a webservice that accepts the 10 input...
49
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.