Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Reference

 

A function argument can be passed by copy, reference or pointer.

 

void f(T t); //t is a copy of the value passed
void f(T &t); //t is the value passed
void f(T* t); //t is a pointer to the value passed passed

 

When a function argument is passed by reference, the function can modify the original variable.

 

#include <cassert>

void swap(int& x, int& y)
{
  const int temp = x;
  x = y;
  y = temp;
}

int main()
{
  int value1 = 1;
  int value2 = 2;
  swap(value1,value2);
  assert(value1==2);
  assert(value2==1);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict