Skip to content
Justice Tech School

References

A reference is another name for a variable. Use it to avoid copies or to modify arguments.

Example
#include <iostream>

int twice(int &n) { n *= 2; return n; }

int main() {
  int x = 5;
  int &r = x;
  r = 10;
  std::cout << x << "\n";
  std::cout << twice(x) << " " << x << "\n";
  return 0;
}

Result

10
20 20

Exercise

Reference symbol.

int r = x;

Quiz Test

Can a reference be null?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.