shitij
July 28th, 2004, 09:50 AM
Is there a way to get more than one value from a function. Presently function just return one value as specified in its return type.
|
Click to See Complete Forum and Search --> : get more than one value from a function shitij July 28th, 2004, 09:50 AM Is there a way to get more than one value from a function. Presently function just return one value as specified in its return type. NigelQ July 28th, 2004, 09:56 AM You can pass in pointers or references to functions. These can be used to return information to the caller. For example: // some routine { int FirstParam = 0; int SecondParam = 0; MultipleReturns(FirstParam, &SecondParam); printf("FirstParam = %d, SecondParam = %d\n", FirstParam, SecondParam); } // The function returning more than one thing void MultipleReturns(int & First, int *Second) { First = 21; *Second = 45; } You'll notice that FirstParam is passed by reference, and SecondParam is passed as a pointer. Just for grins, I declared the routine void (no return value). Hope this helps, - Nigel shitij July 28th, 2004, 10:17 AM Thaknks for your advise. I am able to get the values as I wanted it to be. NMTop40 July 28th, 2004, 11:14 AM You can also return 2 values by using std::pair< T1, T2 > where T1 and T2 are the types. Any more and you can always put them into a struct and return the struct. The other option, as been mentioned above, is for the function to take non-const references (or pointers) and write the values into there. Teddybare July 28th, 2004, 11:29 AM If there are a lot of variables let's say int int MyArray[10]; you can send int* MyArray as an argument to the function and then access the whole array by reference. Ted KevinHall July 28th, 2004, 01:45 PM Another option is to try boost::tuple. YourSurrogateGod July 28th, 2004, 02:19 PM Another option is to try boost::tuple.What do you mean exactly? I've never heard of this. KevinHall July 28th, 2004, 03:59 PM Boost is a library of features that people hope will be added to the C++ standard. Actually, there's more to it than that, but I digress. So you can view boost as a free addon library. Anyway, here is (http://www.boost.org/libs/tuple/doc/tuple_users_guide.html) the documentation for boost::tuple. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |