By default C++ copies arguments — changes inside the function don't affect the original:
void doubleIt(int x) { x *= 2; } // only changes local copy
int n = 5;
doubleIt(n);
cout << n; // still 5!
Add & after the type to make it a reference:
void doubleIt(int& x) { x *= 2; } // modifies the original
int n = 5;
doubleIt(n);
cout << n; // 10
void minMax(int a, int b, int c, int& mn, int& mx) {
mn = min({a, b, c});
mx = max({a, b, c});
}
int lo, hi;
minMax(3, 1, 7, lo, hi); // lo=1, hi=7
Write swap(int& a, int& b) that swaps two values in-place using a temporary variable.
Click "Run" to execute your code.