Function to return two values

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 5
Reputation: touqra is an unknown quantity at this point 
Solved Threads: 0
touqra touqra is offline Offline
Newbie Poster

Function to return two values

 
0
  #1
May 10th, 2008
Can C++ make a function that returns two values ?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: Function to return two values

 
0
  #2
May 10th, 2008
No you cannot. But you can pass the variables by reference so that the changes are reflected in the original variables. For example
  1. void test(int &a,int &b)
  2. {
  3. a=a+1;
  4. b=b+1;
  5. }
  6.  
  7. int main()
  8. {
  9. int c=23,d=6;
  10. test(c,d);
  11. cout<<c<<endl<<d;
  12. cin.get();
  13. cin.ignore();
  14. return 0;
  15. }

Here the values of c and d will be incremented by 1 i.e. changes will be reflected in the original variables.
Last edited by hammerhead; May 10th, 2008 at 5:17 am.
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Function to return two values

 
0
  #3
May 10th, 2008
Alternatively, if you want to return something you could create a struct/class which contains the data you want to return.
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. struct names {
  6. std::string first_name;
  7. std::string last_name;
  8. };
  9.  
  10. names read_from_file( std::ifstream &in ) {
  11. names i_names;
  12. in >> i_names.first_name >> i_names.last_name;
  13.  
  14. return i_names;
  15. }
Probably isn't the best example of how to do it but gives you an idea anyhow.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Function to return two values

 
0
  #4
May 10th, 2008
Alternatively, you can make a class where the return values are fields and the object can be called like a function by overloading the () operator:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct Name {
  5. std::string First;
  6. std::string Last;
  7.  
  8. void operator()()
  9. {
  10. std::cin >> First >> Last;
  11. }
  12. };
  13.  
  14. int main()
  15. {
  16. Name getName;
  17.  
  18. getName();
  19. std::cout << getName.Last << ", " << getName.First << '\n';
  20. }
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Function to return two values

 
1
  #5
May 10th, 2008
c++98 has the convenient std::pair<> .
c++09 also has std::tuple<> .
  1. #include <utility>
  2. #include <algorithm>
  3. #include <tuple>
  4.  
  5. std::pair<int,int> min_n_max( const int* arr, std::size_t N )
  6. {
  7. // invariant: N > 0
  8. int low = arr[0] ;
  9. int high = arr[0] ;
  10.  
  11. for( std::size_t i=1 ; i<N ; ++i )
  12. {
  13. low = std::min( low, arr[i] ) ;
  14. high = std::max( high, arr[i] ) ;
  15. }
  16.  
  17. return std::make_pair(low,high) ;
  18. }
  19.  
  20. std::tuple<int,int,double> min_max_n_avg( const int* arr, std::size_t N )
  21. {
  22. // invariant: N > 0
  23. int low = arr[0] ;
  24. int high = arr[0] ;
  25. int sum = arr[0] ;
  26.  
  27. for( std::size_t i=1 ; i<N ; ++i )
  28. {
  29. low = std::min( low, arr[i] ) ;
  30. high = std::max( high, arr[i] ) ;
  31. sum += arr[i] ;
  32. }
  33.  
  34. return std::make_tuple( low, high, sum/double(N) ) ;
  35. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 5
Reputation: touqra is an unknown quantity at this point 
Solved Threads: 0
touqra touqra is offline Offline
Newbie Poster

Re: Function to return two values

 
0
  #6
May 10th, 2008
I need to write a code which I have a large quantity of values, and from these sample, get the maximum and minimum values. I was thinking of using vectors or arrays but there are too large a quantity...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Function to return two values

 
0
  #7
May 11th, 2008
> .. I have a large quantity of values...
if the values are in a file, pass the file (istream) to the minmax function. in the function, read the values one by one and check for min/max.
if the values are programmatically generated, pass the generator function (or function object) to the minmax function.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC