Copies and references
Watch it run · 1 of 3
A function gets a copy
1#include <iostream>2using namespace std;34void addOne(int x) {5 x = x + 1;6}78void addOneRef(int &x) {9 x = x + 1;10}1112int main() {13 int n = 5;14 addOne(n);15 cout << n << endl;16 addOneRef(n);17 cout << n << endl;18 return 0;19}
Memory
int n
5
Output
(nothing yet)line 13n is 5.
step 1 of 5