Skip to content

Copies and references

Watch it run · 1 of 3

A function gets a copy

1#include <iostream>
2using namespace std;
3
4void addOne(int x) {
5 x = x + 1;
6}
7
8void addOneRef(int &x) {
9 x = x + 1;
10}
11
12int 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