7

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

Naveed
  • 41,517
  • 32
  • 98
  • 131
  • 3
    @Gyom: Mentioning a website, book, duplicate question, or other reference that answers this question is the right way to say that on Stack Overflow, which attempts to *be* TFM. –  Feb 05 '10 at 12:55
  • Yes. You know it provides the latest updates on my questions at one place from good developers. You just need to refresh the page. Is there any way to refresh browser automatically ? – Naveed Feb 05 '10 at 12:59
  • @Naveed: Try asking about that on http://SuperUser.com. :P (Or searching for it already asked.) –  Feb 05 '10 at 13:00
  • @Roger Pate: yes, I should have added STFW :-) http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_value – Gyom Feb 05 '10 at 13:05
  • May be in 2045, When you google any programming related question, the google will just print with anger "TTFSO" ;) – Naveed Feb 05 '10 at 13:14
  • 1
    Duplicate: http://stackoverflow.com/questions/373419/whats-the-difference-between-a-parameter-passed-by-reference-vs-passed-by-value – serhio Feb 05 '10 at 17:28
  • RTFM: http://msdn.microsoft.com/en-us/library/aa903254%28VS.71%29.aspx – serhio Feb 05 '10 at 17:52

6 Answers6

15

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

9

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
  • 167,383
  • 100
  • 513
  • 979
2

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
  • 18,290
  • 7
  • 46
  • 74
2

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 by copy.

Tankiso
  • 21
  • 1
1

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
  • 28,471
  • 6
  • 52
  • 68
0

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
  • 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)? – Simon Gill Feb 05 '10 at 12:46
  • 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. –  Feb 05 '10 at 12:50