1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// compile with: g++ hello.cpp
#include<iostream>
#include<string>
#include<vector>
using namespace std;
// function prototype, & means "pass by reference" - value will be copied back out
// note: just typing & once is less typing, but also if you just look at main you
// won't know if swap actually changes x and y
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main(int argc, char * argv[]) {
cout << "x: "; int x; cin >> x;
cout << "y: "; int y; cin >> y;
cout << "swapping..." << endl;
swap(x, y);
cout << "x: " << x << endl;
cout << "y: " << y << endl;
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX