Difference between value parameter and reference parameter?

Difference between value parameter and reference parameter? This question was once asked by interviewers during my interviews. Can someone tell me the exact difference, which is easy to explain with an example? Is both the link parameter and the pointer parameter the same?

thanks

+3
reference oop parameters
source share
7 answers

Changes to the value parameter are not displayed to the caller (also called "pass by value").

Changes to the link parameter are displayed to the caller ("pass by link").

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 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 that some languages, such as C., do not have. (Of course, you can also treat pointers as values ​​themselves.)

+14
source share

The main difference is whether the transferred object is copied. If it is a value parameter, the compiler should generate such code that changes the function parameter inside the function, does not affect the transmitted source object, therefore it usually copies the object. In the case of reference parameters, the compiler must generate such a code so that all operations are performed on the original transmitted object.

+8
source share

A pointer is a low-level way to represent a link, so passing a pointer (by value) is how languages ​​like C usually reach through semantics.

+2
source share

I think this link can help you Call by Value and Call by Reference . This guy gave a detailed explanation of how this works. I hope this becomes clear.

+2
source share

The difference is quite simple: direct parameters are transmitted by value, and the recipient receives a copy of the transmitted; that if the parameter is changed by the recipient, these changes will not be returned back to the caller. (This is often called appropriate enough, passed by value or copied.

+2
source share

There are basically three types of parameters; pointer, referential and direct.

The difference is quite simple: direct parameters are transmitted by value, and the recipient receives a copy of the transmitted; that if the parameter is changed by the recipient, these changes will not be returned back to the caller. (This is often called appropriately, passed by value or bycopy .

Pointers are also passed by value, but instead of sending the actual value, the caller sends the address of the value. This means that by following this pointer, the receiver can change the argument. Note that changes made to the actual index are still not reflected back to the caller.

The final form invoked by reference is a kind of intermediate point between the two approaches. Essentially, this can be thought of as a pointer that looks like a value.

It should be noted that at the heart of all this, 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; } 
+1
source share

pseudo code:
Pass by value:

 void setTo4(value) { // value is passed by value value = 4; } int x = 1; setTo4(x); // x is still 1 

Follow this link:

 void setTo4(value) { // value is passed by reference value = 4; } int x = 1; setTo4(x); // x is 4 
0
source share

All Articles