Discover how you can return values from functions in this video. By returning a value from your functions you have increased flexibility.
- Growing up, your mom may have asked you to go get the mail so you leave, get the mail and then you bring it back to her. Other times, she may have asked you to wash the dishes so you go and wash the dishes, there's nothing to bring back. That's kind of like the functions we've been creating so far. They do some work and then that's it. But when you need something back, just like how your mom expected the mail, you can use functions that return a value. Let's move over to VS Code to see it in action. Here, on line number one we've created a function and it's called, withdraw money. It takes in, two parameters. The first one is current balance. This stores the current amount of money that you have in your account. The second parameter is named amount. This is the amount that you want to withdraw. Then, on line number two, we have an if statement. The if statement checks to make sure you have enough money in your account to cover the amount that you want withdrawn. If you do, then we come to line number three where we update the current balance with some simple subtraction. We take the current balance and we subtract the amounts. And then finally, on line number four, we print out what the current balance is using this variation of the print function. Here on line number six, we make our call to the withdraw money function. We pass in a 100 for the current balance and 80 for the amount. Let's run the function to check the output. And as you expected, the balance is 20. Now, let's say after we withdraw the money we want to check the new balance later in our program to see if we need to make a deposit. This is where returning a value from our function comes in handy. In Python, when creating a function using the def statement, you can specify what the return value should be with a return statement. A return statement consists of the return keyword and the value that the function should return. Remember, Python has over 30 keywords that it uses to take special actions. Return is one of those keywords. We need to make a few changes in order to take advantage of the return keyword. The first thing we're going to do is on line number three, we'll hit Enter and then we're going to return the current balance. We're going to use the return keyword in order to do that so we'll just type return and then current underscore balance. The next change we need to make is to copy this print statement and remove it outside of our function. Since we're going to use it later, let's not completely delete it. We'll just cut and remove that extra line. Okay, great, we're almost done. Coming down to line number six, since we want to use the value for later, we need to store it inside of a variable. To do that, we'll go to the beginning of the line and we're going to create a variable that we call, balance. And we'll use the equal sign in order to assign it the value from our function. The final thing we want to do is to add an if-else to check and see if we need to make a deposit. So, underneath this function call, let's create our if statement. We're going to check if the balance is less than 50. Then we're going to say, we need to make a deposit. Else, we're going to print out, nothing to see here and that's it. Let's save our file and then run this code. Perfect, we get a message, we need to make a deposit. That's because our balance variable now has the value of 20 so we fall inside of our if statement and it's letting us know that we need to make a deposit. Being able to return values from functions is a cornerstone of programming. I can't wait to see what you're able to build with this new power.
- Writing source code
- Basic statements and expressions
- Working with numbers and strings
- Writing conditional code
- Increasing your efficiency with functions
- Working with comments
- Making decisions in code
- Troubleshooting errors
- Learning about other languages