In this video, Doug Winnie shows how to return values from methods using the return statement. The return statement allows coders to use a method as a value in an evaluation or as a source to assign a value to another variable.
- [Instructor] In addition to accepting values and functions, you can return values from functions. This allows you to use functions like any other variable, because when you run it, it returns a value. You already know two ways to assign values to a variable. The first is to assign a defining number like the number five. This is called a literal. The other way is to use math, or some other function to change a value, like using addition or subtraction. This is called an evaluation. The third is to use a function, but that function needs to return a value.
If we open up the code for this lesson, you'll see that we have a variable that holds a subtotal, and we output that to the console. It doesn't do that much, but what we need to add is a way to calculate the tax. Since tax can be different based on the location, we should create a new function to calculate the tax and use a function parameter to change what the tax rate should be. We can create a function to do exactly that. After our constructor here, let's create a new public static void function called calculate tax, which we are then going to accept a parameter that's going to be a double, which we'll call taxRate.
Inside, we're going to create a new variable, which is going to be a double called tax, and set that equal to be subtotal times taxRate. That is the variable that we're passing into the method. Then we'll access System, out, printlm, and we'll put in the string Tax, add a space, end the string, and then concatenate tax, and then put our semicolon.
Now after our subtotal line, let's execute calculateTax. And we'll pass in a 0.08 double number. In this new method, calculate tax, we pass in the double 0.08 as the tax rate for the transaction. It then calculates the tax based on that and sends it to the console. But we don't do anything with the tax value. We need to use that value to calculate the total. We can do that by changing our function to return a value.
Then when we call a function, it will run and then where the function was called it will replace that with the value that is returned. To change calculate tax to return a value, we need to do two things. First we need to change the type of the method. Currently, it is a void type. This means that it doesn't return anything. You need to type a function or method just like any other variable if you are returning a value. So, we need to change void to double, since we are going to return a double value.
Then we need to add in the return statement at the bottom. And then the value that will be returned out of the method. Instead of calling the function, we can delete this, and directly calculate the total. First, we need to create a variable to hold the total. So create a double, which we'll call total. Then we'll set the equal to the subtotal, and then add in a value that will be returned from the calculate tax function.
We'll then call calculateTax, then pass in our 0.08 double. Finally we can send the total amount to the console. System.out.printlm, string Total, and we'll concatenate total. And then end with a semicolon. If we build in a run, we'll see three lines on the console, with the subtotal, the tax amount that is calculated within the calculate tax function, and the final total amount.
When your program reaches the return statement, it immediately exits the function. So, if there's any code that is after the return statement, it won't be executed. It is always a good idea to have the return statement be the last line in the function. There is one more thing we should do. We built the calculate tax function knowing that the fact that the subtotal amount was within scope of the function. It might not always be that way. So we can set up the calculate tax function to accept two values. To do that, we simply add a comma after the first one and then add the parameter variable name for the second one.
We'll add in another one called double, and then we'll call it amountToTax. Now we can update our references inside the method. So instead of subtotal, we want amountToTax. In our code then we now need to adjust the call to calculate tax, so we'd send in two values instead of just the one tax rate. I'll do that by putting in another comma, and then passing in subtotal. Now the calculate tax function will work with both values.
Calculate the tax and then return that value to the same location where calculate tax was called. It then adds it to subtotal and assigns a sum to the variable total. Methods can work with values in two ways. It can accept values using parameters that are variable scoped to the code in the method, and they can return values to the location where it was called using the return statement.
- The history of Java
- Setting up your development environment
- Working with values and variables
- Using methods and functions
- Capturing user input
- Creating conditional tests
- Using loops
- Creating and changing arrays
- Object-oriented programming in Java
- Defining permissions
- Extending classes