Golang Call by Reference and Call by Value
Overview
Call by value and call by reference is the most common topic in any programming language. Call by value in simple terms is calling a function by passing a value, on the other hand, call by reference means calling a function by passing a reference of a value. In this article, we will cover all about the call by value and call by reference in the Go language.
Introduction to Parameters in Golang
In the Go language, parameters also known as a formal argument are a kind of variable used as a subroutine for referring to one piece of data provided in the form of input to the subroutine. The pieces of data are called arguments through which the subroutine is invoked. The ordered list of parameters is generally included in the subroutine definition. Hence, each time a subroutine is called the argument for that call is evaluated and the resulting values are then assigned to the corresponding parameters.
Look at the example given below to understand this much better:
Here x, y are the parameters while when the value let's say (2,3) is passed then it is called arguments.
Call by Value
The call by value is used for passing an argument to a function by copying the actual value of an argument into the formal parameter of the function. In this case, any changes that are made to a parameter inside a function do not affect the argument of the function.
The Go language, by default, uses call by value method for passing an argument. This, in general, means that the code that resides within the function cannot alter the argument that is used for calling the function. Consider an example given below which uses the swap() function for swapping the two values given below:
Now let us call the function swap() by passing the actual values in the following example:
Copy and paste the above code into a C fileandexecute` it, you will see the following output on the screen:
Output:
Explanation: In this example, we can see that there has been no change in the value of a and b even though the values are changed inside a function.
Call by reference
Call by reference method is a way of passing the arguments to a function by copying the address of the argument in the formal parameter. In the function, the address of the parameters is used for accessing the argument used in the function call. This means that any changes made to the parameters affect the argument.
In pass-by-reference, the pointer of the argument is passed to the function, similar to any other value. To understand this better, let's take an example of swapping two values.
In this example, a swap() function is allied by passing the reference of the variable.
Copy and paste the Go code in the file, compile, and execute it, and the following output is seen on the screen:
Output:
Explanation: In this example, it is seen that values are been swapped and are reflected outside the function unlike call by value method in which the value does not reflect outside the function.
How to do Call by Reference in Golang
In call by reference, a pointer is used to point to a variable where the memory address of other variables is stored. It points to the memory location and retrieves the value stored at that memory location. These variables are generally termed as a special kind of variable since it is declared as a variable but with the * also known as the dereferencing operator.
To understand how the call by reference works, let’s look at the example given below:
Explanation: In this example, when BrokenSwap(x, y) is called, there is no effect on the output since the function retrieves and manipulates a private copy of data instead of an actual copy. On the other hand, when RealSwap(&x, &y) is called, the values are swapped and reflected in the output. This is due to the address of the variable that the pointer is pointing to instead of just a value.
Difference Between Call by Values and Call by Reference
Call by value | Call by reference |
---|---|
In call by value, when calling a function, a value of the variable is passed to it. | In call by references, instead of passing the value of the variable, the address also known as the location of the variable is passed to the function. |
In this method, the value of each variable in a function call is copied into the corresponding variable which is also known as a dummy variable. | In this method, the address of an actual variable in a function call is copied into a dummy variable of the function that is called. |
In this method, any changes made to a dummy variable in a function call does not affect the actual variable of the function call. | In this method, by using the address of the variable, we can have access to the actual variable, and therefore manipulation becomes easy. |
In call by value, the actual values of variables cannot be altered through a function call. | In call by reference, the values can be altered through a function call. |
Simple technique is used for passing the variable. | Pointer variables are used to define and store the address of values in a variable. |
If a change is made in a copy of a variable then it cannot modify the value outside the method. | Changes made in the variable can affect the value even outside of the variable. |
Advantages and Disadvantages of Call by Value and Call by Reference
Advantages of call by value:
- This method does not change the original value of the variable which helps in preserving the data
- In a function call, the actual constant of the argument is never affected
- The actual value of the argumen is passed in the formal argument and hence any changes that are made in the formal argument do not affect the `real content
Disadvantage of Call by Value:
- Changes in the actual parameter can modify the corresponding variable argument
- The argument must be a variable in this method
- The variable name can’t be changed directly in a function body
- This method is not memory efficient since two topics are created for the same variable
Advantages of Call by Reference:
- No duplicate data is created for holding a value which means it helps to save memory
- The data is processed fast as there is no copy of the argument
- It helps avoid the changes done by mistake
- The function call changes the value of the argument
Disadvantage of Call by Reference:
- For call by reference, the method has to take care that the value that the pointer is pointing to is not null
- There is an issue with references made by the pointer especially with the multi threading programs and lambda expression
Conclusion
- Call by value and call by reference is the most common topic in any object-oriented language. Call by value in simple terms is calling a function by passing a value, on the other hand, call by reference means calling a function by passing a reference of an object.
- In the Go language, parameters also known as a formal argument are a kind of variable used as a subroutine for referring to one piece of data provided in the form of input to the subroutine.
- The call by value is used for passing an argument `to a function by copying the actual value of an argument into the formal parameter of the function. In this case, any changes that are made to a parameter inside a function do not affect the argument of the function.
- Call by reference method is a way of passing the arguments to a function by copying the address of the argument in the formal parameter. In the function, the address of the parameters is used for accessing the argument used in the function call.