Difference between value parameter and reference parameter?

Changes to a value parameter are not visible to the caller (also called “pass by value”).

Changes to a reference parameter are visible to the caller (“pass by reference”).

C++ example:

void by_value(int n) { n = 42; }
void by_ref(int& n) { n = 42; }

void also_value(int const& n); // Even though a reference is used, this is
// semantically a value parameter---though there are implementation
// artifacts, like not being able to write "n = 42" (it's const) and object
// identity (&n here has different ramifications than for by_value above).

One use of pointers is to implement “reference” parameters without using a special reference concept, which some languages, such as C, don’t have. (Of course you can also treat pointers as values themselves.)

Leave a Comment