Reference Parameters void reorder(int a, int b) { if (a<=b) return; int t = a; a = b; b = t; } void main() { int x = 34, y = 23; cout << "x, y are " << x << ", " << y << "\n"; // I want (x, y) to keep the same two values, but arranged so that x <= y reorder(x, y); cout << "x, y are " << x << ", " << y << "\n"; // nothing happens! } // In reorder, a and b are just ordinary temporary variables. // They are initially set up with the values of x and y, but // have no connection after that. Reorder does correctly swap // around a and b, but that is no use. x and y are unaffected. // The solution is this void reorder(int & a, int & b) { if (a<=b) return; int t = a; a = b; b = t; } // The only change is in the first line. Ampersands '&' have appeared // before the names a and b. This means that a and b are reference // parameters, and not just temporary independent variables. // now, when reorder(x, y); // is used, a is just another name for x, and b is just another name // for y. When a and b are swapped around, it is really x and y that // are getting swapped around. So this version works. void add_ten_percent_to(double x) { x = x + x*0.1; } void main() { double pay = 155.23; cout << "pay = " << pay << "\n"; add_ten_percent_to(pay); cout << "after adding 10%, pay = " << pay << "\n"; } // This one doesn't work, and for the same reason. It prints pay = 155.23 after adding 10%, pay = 155.23 // The correct version is void add_ten_percent_to(double & x) { x = x + x*0.1; }