|
-
July 28th, 2004, 09:50 AM
#1
get more than one value from a function
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.
-
July 28th, 2004, 09:56 AM
#2
You can pass in pointers or references to functions. These can be used to return information to the caller.
For example:
Code:
// 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
-
July 28th, 2004, 10:17 AM
#3
achieved retrieving values
Thaknks for your advise. I am able to get the values as I wanted it to be.
-
July 28th, 2004, 11:14 AM
#4
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.
-
July 28th, 2004, 11:29 AM
#5
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
-
July 28th, 2004, 01:45 PM
#6
Another option is to try boost::tuple.
-
July 28th, 2004, 02:19 PM
#7
 Originally Posted by KevinHall
Another option is to try boost::tuple.
What do you mean exactly? I've never heard of this.
-
July 28th, 2004, 03:59 PM
#8
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 the documentation for boost::tuple.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|