Functions have the ability to change their behavior based on their parameters and arguments. Learn what they are and how they differ in this video.
- Growing up, my siblings and I loved to go to the carwash. And depending on how much money my parents were willing to pay, we could have a different experience each time. For $6, we would get the most basic wash, white foam, one rinse and a drip dry. But if they were willing to spend $12, then we could get tricolor foam, two rinses Just like the carwash, in programming, we often want to provide different outputs to our users based on some input. Functions allow us to do this, in fact, let's go over to VS Code to see a function that simulates our carwash experience. We're here in 05_03_begin. We have this function defined on line number one, wash_car, and then we have our open and close parentheses Inside, we have our carwash steps. First, we have the steps for the Platinum car wash on lines two through four. Then, down on lines six through eight, we have the steps for the Basic car wash. Let's go ahead and call this function using its name. We'll do that down here on line number 10. Wash_car and then a set of open and close parentheses. and go down to Run Python File in Terminal. And you probably guessed it, when the amount paid is $12 and the Basic steps only if the amount paid is $6? How do you think we can do that? I hope the first thing that came to your mind is an if statement. Let's pretend we have the amount paid stored in a variable named amount_paid, So let's close this down and then, we'll come back inside of our function and we'll start right on line number two, we'll add our if statement. So, if the amount paid is equal to 12, this is for the Platinum wash, Remember, we have to indent the statements, that all three of these print statements belong to this if. Next, we'll come down to line number seven and we'll create another if statement, this time it's for the Basic car wash, if the amount_paid is equal to six, then we're going to do the steps in the Basic car wash. Once again, make sure you indent the statements that you want to include inside your if. So now, let's save it and run our file again. And look, we get an error, Python is letting us know so we need to do something to bring this variable inside of our functions world. In order to do that, let's close down this window and we're going to come up to where we've defined our function, inside of our open and close parentheses, then we're going to include amount_paid, this is how we let our function know Let's save our code now. In programming, this variable has a special name, a parameter. to change their behavior based on some input. Let's go ahead and run our code again. But look, we get an error, but what does that mean? What is a positional argument? Well, similar to how we have parameters as the name for the variables we use in our function definition, we have arguments as the name we use for the values that we give to our functions. And we give our functions arguments by placing a value between the open and close parentheses in the function call. Let's do that now. and then here, on line number 12, where we call our function, let's give it the number six in between the open and close parentheses, And indeed, we get all the steps for the Basic car wash. On your own, provide 12 as the argument to the function and enjoy the Platinum wash experience. One thing to note is that sometimes parameters and arguments are used interchangeably, you'll have to rely but don't despair, the key thing to know is that functions have the ability to change their behavior based on their parameters and arguments.
- 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