| | |
Function to return two values
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
No you cannot. But you can pass the variables by reference so that the changes are reflected in the original variables. For example
Here the values of c and d will be incremented by 1 i.e. changes will be reflected in the original variables.
C++ Syntax (Toggle Plain Text)
void test(int &a,int &b) { a=a+1; b=b+1; } int main() { int c=23,d=6; test(c,d); cout<<c<<endl<<d; cin.get(); cin.ignore(); return 0; }
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.
All generalizations are wrong. Even this one.
Alternatively, if you want to return something you could create a struct/class which contains the data you want to return.
Probably isn't the best example of how to do it but gives you an idea anyhow.
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <fstream> struct names { std::string first_name; std::string last_name; }; names read_from_file( std::ifstream &in ) { names i_names; in >> i_names.first_name >> i_names.last_name; return i_names; }
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> struct Name { std::string First; std::string Last; void operator()() { std::cin >> First >> Last; } }; int main() { Name getName; getName(); std::cout << getName.Last << ", " << getName.First << '\n'; }
If at first you don't succeed, keep on sucking until you do succeed.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
c++98 has the convenient
c++09 also has
std::pair<> .c++09 also has
std::tuple<> . c++ Syntax (Toggle Plain Text)
#include <utility> #include <algorithm> #include <tuple> std::pair<int,int> min_n_max( const int* arr, std::size_t N ) { // invariant: N > 0 int low = arr[0] ; int high = arr[0] ; for( std::size_t i=1 ; i<N ; ++i ) { low = std::min( low, arr[i] ) ; high = std::max( high, arr[i] ) ; } return std::make_pair(low,high) ; } std::tuple<int,int,double> min_max_n_avg( const int* arr, std::size_t N ) { // invariant: N > 0 int low = arr[0] ; int high = arr[0] ; int sum = arr[0] ; for( std::size_t i=1 ; i<N ; ++i ) { low = std::min( low, arr[i] ) ; high = std::max( high, arr[i] ) ; sum += arr[i] ; } return std::make_tuple( low, high, sum/double(N) ) ; }
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> .. 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.
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.
![]() |
Similar Threads
- comparing return values of subroutines (Assembly)
- Can a function return 2 values? (C)
- loop in main function to an "if" statement (C++)
Other Threads in the C++ Forum
- Previous Thread: Create Folder
- Next Thread: Binary Search Trees - Need Help Indicating Node Level
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






