to return 2 arguments to main?

how ro return 2 values x and y from function to main? then to add these values to the a[x], and a[y]. it means to add x and y from function to the array
To return more than one value from a function:

1
2
3
4
5
6
7
8
9
10
void some_func( int& x, int& y ) {
   x = 5;
  y = 4;
}

int main() {
    int a, b;
    some_func( a, b );
    // a == 5, b == 4 now
}

You can have two arguments passed by reference to your function used for output

eg:
1
2
3
4
5
6
void return2values(/*some arguments*/, int &result1, int &result2)
{
    //some computations
    result1 = something;
    result2 = something_else;
}


EDIT: typed too slow...
Last edited on
You could use pointer arguments, too. Then it will be obvious to the users of the function that the values may be modified (noticeable from the & in the calls).

Although less common, you could also return a std::pair or a boost::tuple. It all depends on how you want your code to be.
thanks all ur replies
Topic archived. No new replies allowed.