Difference Between Pass By Reference And Pass By Value?
Pass By Reference:
In Pass by reference address of the variable is passed to a function. Whatever changes made to the formal parameter will affect the actual parameters
- The same memory location is used for both variables. (Formal and Actual)
- It is useful when you are required to return more than 1 value.
Pass By Value:
- In this method value of the variable is passed. Changes made to formal will not affect the actual parameters.
- Different memory locations will be created for both variables.
- Here there will be a temporary variable created in the function stack that does not affect the original variable.
In the case of pass-by value, the change in the sub-function will not cause any change in the main function whereas in pass-by-reference the change in the sub-function will change the value in the main function.
Pass by value sends a COPY of the data stored in the variable you specify, pass by reference sends a direct link to the variable itself. So if you pass a variable by reference and then change the variable inside the block you passed it into, the original variable will be changed. If you simply pass by the value, the original variable will not be able to be changed by the block you passed it into but you will get a copy of whatever it contained at the time of the call.