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 defined value, like the number five. That is a called a literal. The other way it 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, we will see that we have a variable that holds a subtotal and we output that subtotal to the console log. 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. Let's go ahead and create a new method that's public and static, void. And we'll call it calculate tax. And it's going to accept a double number called tax rate. Inside, we're going to create a new variable called double and we'll call that tax. This value is going to be the existing subtotal that's been created, that's been defined outside of the main function, and we'll multiply that by the tax rate.
Then, we'll access the console. And we'll write a line that's going to be a string, that's going to provide the value of the tax. Then, inside of the main function, we can create a new line where we can calculate the tax and then pass in the tax rate, which we'll do as .08. 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 that to the console. But we don't do anything with the tax value. We need to use that value to calculate the overall total. We can do that by changing our function to return a value. Then, when we call the function, it will run. And then, where the function was called, it will replace it 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's a void type. This means that it doesn't return anything. You need to type a function or a method just like any other variable, if you are returning a value. So we need to change void to double, since we are calculating a double value. Then, inside of the function, we need to add a return statement. And then, the value that will be returned out of the method. Now 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. We'll call this double total and we'll set this equal to subtotal plus calculate tax 0.08. And then, finally, we can go to the console, write a new line which will be a string containing the overall total. If we start without debugging, we will see three lines in 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 will immediately exit the function. So if there is 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 the fact that the subtotal amount was within the scope of the function. It might not always be this 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. It's going to be a double and we'll call it amount to tax. In our code then, we then need to adjust the call to calculate tax so we send in two values instead of one. We'll pass in the subtotal variable. And then, inside of calculate tax, we're now going to replace subtotal with amount to tax.
Now the calculate tax function will work with both values. Calculate the tax and then return that value to that same location where calculate tax was called. It will then add it to the subtotal and assign that sum to the variable total. Methods can work with values in two ways. It can accept values using parameters that are variables scoped to the code in the method. They can also return values to the location where it was called using the return statement.
Programming expert Doug Winnie starts by sharing the history of C# to give you context into its purpose and beneficial uses. Then he walks through a sample of code showing how to run a program using the Visual Studio IDE. After warming up with a sample, he dives into working with values, variables, methods, and custom functions. Next, he shows how to capture values input by site users, managing different variable types, building compound conditional tests, and using loops with arrays. In the final five videos, he covers the basics of object-oriented programming including classes, objects, and permissions.
- C# history, uses, and terminology
- Setting up your PC or Mac
- Working with values and variables
- Using methods to repeat actions
- Customizing functions with parameters
- Managing scope, rules, variables, values, and functions
- Capturing input from a user
- Creating conditional tests
- Using loops
- Creating arrays to store groups of values
- Collections and lists
- Making your own classes and objects
- Defining permissions for class members
- Extending classes