2 numbers in one function

How do you return 2 numbers in one function? Thanks for answering.

#include <iostream>

using namespace std;

int swap(int &, int &);

int main () {

int a, b;
cout << "Please input 2 numbers" << endl;
cin >> a >> b;

cout << "The 2 numbers swapped are " << swap(a, b) << endl;

return 0;
}

int swap(int &x, int &y) {
int t = x;
x = y;
y = t;

return x;
return y;
}
You can return them through the function parameters, provided you make these references (as you have done, correctly).

Since nothing else needs to be returned, declare and define a void function:
void swap(int &x, int &y)
and remove the lines
1
2
return x;
return y;

(because they are being returned through the function parameters, not the return value of the call).

PLEASE USE CODE TAGS
Look at http://www.cplusplus.com/reference/algorithm/swap/
How does it "return" values?


A function can return at most one object. The object could be a primitive type or a class/struct. The latter can have multiple member variables.
Topic archived. No new replies allowed.