Computer Applications
What is the principal reason for passing arguments by reference ? In a function call, what all data items can be passed by reference ?
Answer
The principal reason for passing arguments by reference is that we want the modifications done by the called method to reflect back to the original variables (i.e., actual parameters). For example, a method that swaps the values of two variables. In this case, we will pass an object containing the two integers to the method so that the swapped variables are visible in the calling method.
The reference data types — objects and arrays, are passed by reference.
Related Questions
What is the principal reason for passing arguments by value ?
When an argument is passed by reference,
- a variable is created in the function to hold the argument's value.
- the function cannot access the argument's value.
- a temporary variable is created in the calling program to hold the argument's value.
- the function accesses the argument's original value in the calling program.
What is the role of a return statement in a method ?
What are the three types of functions in Java?