Reference parameter in C++

Level 392 keystrokes

doubleValue takes its parameter by reference (int&), so changing x inside the function changes the caller's num: 5 becomes 10.

C++ code

// int& makes x a reference: another name for the variable passed in, not a copy
1void doubleValue(int& x) {
// Doubles the value; this writes directly to the caller's variable
2 x = x * 2;
3}
4
5int num = 5;
// Passes num by reference; after the call num is 10
6doubleValue(num);
// Prints 10
7cout << num << endl;

Output

10
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • ;× 4
  • <× 4
  • (× 2
  • )× 2
  • =× 2
  • {× 1
  • }× 1
  • *× 1
  • &× 1

More C++ examples at level 3

← C++ typing practice