The call by value technique sends the function code simply the value of a variable. Any changes in the value of a variable within that function have no effect on the variable’s initial value. We send the variable itself as an argument in the call by reference method, and any change in the value of a variable impacts the original value of that variable.
What does the Call by Reference mean?
The call by reference technique copies an argument’s address into a formal parameter. The address is utilised to obtain the actual argument used in the function call in this method. It signifies that any modifications to the parameter have an impact on the passing argument.
The memory allocation is the same as the real arguments in this procedure. The function performs all of its operations on the value stored at the location of the real parameter, and the updated value is saved at the same address.
Example
Public static void main(string args[]) {
int a = 10;
System.out.println(“Before call Value of a = “, a);
Void increment();
System.out.println(“After call Value of a = “, a);
}
Void increment(int x) {
int x = x + 1;
}
Output
Before call Value of a =10
After call Value of a =11
What does the Call by Value mean?
The call by value technique transfers the value of an argument into the function’s formal parameter. As a result, changes to the main function’s parameter have no effect on the argument. The values of actual parameters are transferred to the function’s formal parameters in this parameter passing mechanism, and the parameters are kept in various memory locations. As a result, any changes performed inside functions are not reflected in the caller’s real arguments.
Example
void main() {
int a = 1,
void increment(int);
Cout << “before function calling” << a;
increment(a);
Cout << “after function calling” << a;
getch();
void increment(int x) {
int x = x + 1;
Cout << “value is” << x;
}
Output
before function calling 1
value is 2
after function calling 1-0
Call by Value and Call by Reference: Difference
Call by Value | Call by Reference | ||
“Call By Values” is a term used when passing values to a function by copying variables | Instead of duplicating the values of variables, the address of the variables is utilised when calling a function in a programming language; this is known as “Call By References” | ||
A copy of the variable is supplied to this function | This method takes a variable as input | ||
Changes to a variable’s copy never affect the value of the variable outside the function | The value of the variable outside the function is affected by changes in the variable | ||
Allows you to make no modifications to the variables themselves | Allows you to use function calls to alter the values of variables | ||
The values of variables are passed using a simple technique | To hold the address of variables, pointer variables are necessary | ||
The original value has not been changed | The original value has been modified | ||
Actual and formal arguments | In the same memory region, actual and formal arguments will be formed | ||
Actual arguments are safe since they can’t be changed by accident | Actual debates are not secure Because they might be mistakenly changed, you must be cautious while using parameters operations | ||
Many computer languages, such as C++, have this as the default like PHP, C# and Visual Basic NET | Most programming languages, such as JAVA, support it, although not by default |
Advantages of Using the Call-by-Value Method
The approach preserves data by not changing the original variable.
- When a function is called, it should never change the contents of the parameters
- The value of the actual arguments is passed on to the formal arguments, therefore any modifications to the formal argument have no effect on the real-world situations
Advantages of Using the Call-by-Reference Method
- The function has the ability to modify the argument’s value, which is quite handy
- It does not generate duplicate data when just one value is stored, allowing you to conserve memory
- There is no duplicate of the argument in this procedure
- As a result, it is processed relatively quickly
- Assists you in avoiding modifications made by accident
- A person reading the code has no idea that the function’s value can be changed
Disadvantages of Adopting the Call by Value Approach
- Changes to the actual parameters can affect the argument variables as well.
- Arguments must be variables in this procedure.
- A variable in the body of a function cannot be changed directly.
- Arguments can sometimes be complicated phrases.
- The fact that two copies of the identical variable are being made wastes memory.
Disadvantages of Using the Call by Reference Method
- Non-null assurance is strong. If a function accepts a reference, it must ensure that the input is not null. As a result, no null check is required.
- Because the function is passed by reference, it is no longer pure conceptually.
- With references, a lifetime guarantee is a major concern. When working with lambdas and multi-threaded applications, this is very risky.
Conclusion
Depending on what is supplied, C++ and Java employ both ways. Use the ‘call by value’ technique if you just want to send the variable’s value, and the ‘call by reference’ option if you want to observe the change in the variable’s original value.