You saw various examples where you passed values into the function and the function performed an action based on these values. All the functions you saw so far were not interfering with the original variable.
Can a function modify the variable passed into it? Let’s find out.
Note: You can download the code used in the video from below :
Hi. You saw some examples of functions in the previous lectures. Now let's explore the mechanism of how they actually work. Till now you saw functions that use the values passed into them to give the result you want. But there is more to this that we will use in this lecture. Can you use a function to change the values passed into it? Say you have to swap two values. Can you use a function where you pass the values to be swapped into the function and the function returns the swap value? Let's try a code from your understanding till now. As you can see, we have two numbers here NUM one and NUM two with values ten and 16 respectively. We are then calling a simple swap function which should help us interchange these values. So with the help of a temporary variable, I am swapping variable two with variable one. But what exactly would happen here? Do the values really change? Let's test this now. Number one and number two values still remain the same here even though we call a swap function. What could have gone wrong? Well, it didn't work the way we thought it would. The initial values remain the same. So the swap function did not change the variables at all. Let's examine what happened. Java actually made copies of the variables NUM one and NUM two and passed those copies to the swap function as parameters variable one and variable two. Therefore, in the function where it swapped variable one and variable two, the function is actually swapping copies of number one and number two rather than the actual variables NUM one and NUM two themselves. And that's why NUM one and NUM two remain unchanged even after you call the function. The reason this happens is because for simple data types java passes a variable by value. The values are copied into the parameters of the function and the function performs its action based on these copied values, leaving the main variables untouched.
In order to understand this, imagine you're trying to send a message across a river by a boat. The areas beyond the river is unknown and you probably don't want to travel there by yourself. Here you are equivalent to the variable. Your message is the value and the boat is equivalent to the function. Now suppose that instead of you rowing the boat yourself, you send a recorded message which is essentially a copy of your message. The value of the recorded message reaches the other side while you, the variable, remains safe on this side of the river. Hence the task is done and you also remain safe. This is similar to pass by value. The variable remains unchanged. Only a copy of its value is taken to execute the function. This brings us to an interesting question how do you pass variables into the function instead of their copies? Although it is beyond the scope of discussion at this point you may refer to the links provided below.
Functions use values passed into them to give the desired result
Functions can be used to change the values passed into them
Passing values by value means Java makes copies of the variables and passes them to the function as parameters
The function works with copies of the values, leaving the main variables untouched
Pass by value is like sending a recorded message across a river, leaving the variable unchanged
To pass variables into a function instead of their copies is beyond the scope of this lecture.
So, you learnt that Java uses ‘pass by value’, i.e. the values are copied into the parameters of the function, and the function performs its action based on these copied values, leaving the main variables untouched.
Now you may be wondering how to use functions to modify the variables. You can achieve the desired results by returning the modified values using a function, storing them in a temporary variable and then reassigning them to the concerned variables.
There is another mechanism called ‘pass by reference’ in some programming languages, using which, variables can be modified using functions. Although Java doesn’t support pass by reference, you will learn to achieve the desired results in further modules. You may visit the links provided below to know more.