Home >Web Front-end >JS Tutorial >Pass By Value vs. Pass By Reference, Does It Matter?
Pass by value vs. pass by reference: an in-depth discussion
In my first technical interview, I was asked this question: What is the difference between passing by value and passing by reference? My answer is: Pass by value passes a value, pass by reference passes a reference. While this answer is basic and even superficial, it's not entirely wrong and just needs further explanation.
Value passing: The value of the variable is copied and passed to the function as a parameter. The function uses this copied value to perform various operations, and the value of the original variable remains unchanged . Almost all known programming languages, including JavaScript and Java, use passing by value.
Passing by reference: A reference refers to a variable that points to a value in memory. Think of it as a pointer. It is passed to the function as a parameter and it is given an alias (alias reference) using the parameter. The function performs various operations using this parameter, which provides access to the memory location of the value. Therefore, any operation using this parameter will affect the value of the original variable, and the value of the original variable will be changed. Only a few programming languages use pass by reference, for example C has this capability, but even in C the default mode is pass by value.
Value passing example:
The following example uses JavaScript, which strictly follows value passing, regardless of whether it is a primitive type or a reference type (object). Here is a classic example of a swap function:
<code class="language-javascript">const firstPrimitive = 1; //传递给firstArgument的原始值 const secondPrimitive = 2; //传递给secondArgument的原始值 const firstObject = {name: "first", reason: "Test pass-by-value"}; //传递给firstArgument的对象 const secondObject = {name: "second", reason: "Test pass-by-value"}; //传递给secondArgument的对象 function swap(firstArgument, secondArgument){ const placeholder = secondArgument; secondArgument = firstArgument; firstArgument = placeholder; console.log('当前位于函数内部。'); console.log('firstArgument = ', firstArgument, ', secondArgument = ', secondArgument, '\n'); } console.log('函数执行前。'); console.log('firstPrimitive = ', firstPrimitive, ', secondPrimitive =', secondPrimitive); console.log('firstObject = ', firstObject, ', secondObject =', secondObject, '\n'); swap(firstPrimitive, secondPrimitive); console.log('当前位于函数外部。'); console.log('firstPrimitive = ', firstPrimitive, ', secondPrimitive =', secondPrimitive, '\n'); swap(firstObject, secondObject) console.log('当前位于函数外部。'); console.log('firstObject = ', firstObject, ', secondObject =', secondObject, '\n');</code>
After executing this code, the results are as follows:
<code>函数执行前。 firstPrimitive = 1 , secondPrimitive = 2 firstObject = { name: 'first', reason: 'Test pass-by-value' } , secondObject = { name: 'second', reason: 'Test pass-by-value' } 当前位于函数内部。 firstArgument = 2 , secondArgument = 1 当前位于函数外部。 firstPrimitive = 1 , secondPrimitive = 2 当前位于函数内部。 firstArgument = { name: 'second', reason: 'Test pass-by-value' } , secondArgument = { name: 'first', reason: 'Test pass-by-value' } 当前位于函数外部。 firstObject = { name: 'first', reason: 'Test pass-by-value' } , secondObject = { name: 'second', reason: 'Test pass-by-value' }</code>
Note that inside the swap function, the two values are swapped; however, outside the function, the values remain the same.
Example of passing by reference:
The following example uses C and can be implemented by passing by reference. This can be achieved using the address operator (&). Again, it uses a version of the swap function:
<code class="language-c++">#include <stdio.h> void swap(int &i, int &j) { int temp = i; i = j; j = temp; } int main(void) { int a = 10; int b = 20; swap(a, b); printf("A is %d and B is %d\n", a, b); return 0; }</code>
The output result is:
<code>A is 20 and B is 10</code>
Here the values of a and b are changed because they are used as references.
Summary: Pass by reference means that the function receives the memory address of the variable (by reference), allowing it to modify the original variable directly; while pass by value means that a copy of the variable value is passed without modifying the original variable .
Is this important?
Most programming languages use passing by value, and we seem to have no choice. So, how does this apply? Here’s what I found:
Comments welcome. Thanks.
Reference materials: Is JavaScript pass by reference? Pass by reference (C only)
The above is the detailed content of Pass By Value vs. Pass By Reference, Does It Matter?. For more information, please follow other related articles on the PHP Chinese website!