When you write code, how do you know that you need to use value or reference? Please show with an example. I know reference is two way communication but it is not clear to me. Thanks
We should use value when we don't want the changes done to formal functional arguments to be reflected back to the actual arguments.
For example, when we want to want to find the maximum of two numbers using a function which accept two integers, we write a function for the same. In this case, we should used pass by value since we don't want the changes to the formal arguments to be reflected back to the actual argument.
Now, let us consider another function which accepts an array and then sorts the same array. In this case, we should use reference since the actual array argument should be sorted after the control returns to the callee function.
Get Answers For Free
Most questions answered within 1 hours.