views:

236

answers:

5

Difference between value parameter and reference parameter ? This question is asked sometime by interviewers during my interviews. Can someone tell me the exact difference that is easy to explain with example? And is reference parameter and pointer parameter are same thing ?

Thanks

+4  A: 

The main difference is whether the object passed is copied. If it's a value parameter the compiler must generate such code that altering the function parameter inside the function has no effect on the original object passsed, so it will usually copy the object. In case of reference parameters the compiler must generate such code taht all operations are done on the original object being passed.

sharptooth
+8  A: 

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.)

Roger Pate
+1 Much shorter than my answer, still the same amount of information.
sharptooth
Or was shorter before the example was included. :)
Roger Pate
+1  A: 

A pointer is a low-level way of representing a reference, so passing a pointer (by value) is how languages like C typically achieve pass by reference semantics.

Kylotan
A: 

Pseudocode:
Pass by Value:

void setTo4(value) { // value is passed by value
    value = 4;
}

int x = 1;
setTo4(x);
// x is still 1

Pass by Reference:

void setTo4(value) { // value is passed by reference
    value = 4;
}

int x = 1;
setTo4(x);
// x is 4
dbemerlin
What's the difference between void setTo4(value) and void setTo4(value). Shouldn't the Pass by reference example function be void setTo4(ref int value)?
somori
it is pseudocode, no language specific syntax applies. He didn't mention any language and some languages pass by reference per default, others pass by value. Notice that i did not specify the type of the parameter for setTo4 so it might as well be an object that gets passed by reference. This is an example, not a language specific implementation.
dbemerlin
+1 Anyway (x is still 1) and (x is 4) clears a difference.
NAVEED
+1  A: 

There are basically three kinds of parameters; pointer, reference and direct.

The difference is pretty simple: direct parameters are passed by value, and the receiver receives a copy of what is passed; meaning that if the parameter is modified by the receiver, these changes will not be reflected back to the caller. (This is often called, appropriately enough, pass by value, or bycopy.

Pointers are also passed by value, but rather than sending the actual value, the caller sends the address of the value. This means that by following this pointer, the receiver can modify the argument. Note that changes made to the actual pointer still aren't reflected back to the caller.

The final form, call-by-reference, is sort of a middle ground between these two approaches. Essentially it can be thought of as a pointer that looks like a value.

It is worth mentioning that at the core of it all, parameters are always passed by value, but different languages have different ways of implementing reference semantics (see Kylotans answer).

// Example using C
// bycopy
int multiply(int x, int y) {
  return x * y;
}

void multiply_p(int *x, int y) {
  *x *= y;
}


int main () {
  int i, j, k;
  i = 20;
  j = 10;
  k = multiply(i,j); // k is now 200
  multiply_p(&i, k); // i is now 4000 (200 * 20)
  return 0;
}
Williham Totland
+1 it is nice way of teaching, thanks.
NAVEED