 Welcome back everyone. This is Brian. We're going to continue our journey into Python three with an introduction to functions So first off, what is a function? It's a block of code which only runs when it is called So far all the code we've written runs immediately. I mean as soon as Python reads it. It runs it This is going to change that we're gonna write some code and we are going to decide when it runs This is another fundamental building block of programming and there's a lot of theory attached to it And there's a lot of names and terminology. So we are going to split this into multiple videos Some key takeaways from this video though is that you can pass data known as a parameter or an argument into a function You're gonna hear these two used interchangeably It gets really confusing. So down here. I put some notes the difference between a parameter and an argument a function parameter is the names listed in the functions definition Where an argument is the real value That's just confusing. Why don't they just call it like a name or a value? But instead they've got a call it parameter argument and everyone even myself gets these mixed up You'll hear some videos where I'll call it a parameter and some videos where I'll call it an argument And I've heard people with vastly more programming experience than me switch these around as well. It's just that confusing Another major takeaway is that a function can return data as a result Turn is actually keyword and we're gonna cover that Diving into theory land just for a moment here so far. We've worked with statements And what do I mean by statements? We've done something like this so far We'll have a statement a statement a statement and Python just reads it from the top down for example We would like print something Maybe do a while loop inside that while loop we could have like an if statement And it's just kind of read like a book straight from the top down But now what we're going to do is introduce a function, which is a separate block code It's not going to run unless we specifically tell Python go run this code and a function Can call other functions and so on and so on The end result now is that we can Fundamentally decide how we want our program to run and how we want it to behave We can do some pretty complex programming logic using this another key takeaway here is We are subtly introducing the concept of scope Everything that we've done so far has been on what's called the global scope and each function each block of code Has its own scope scope is something we're going to dive into detail in a future video But just understand we are subtly introducing this So let's test this let's go ahead and define a function We're gonna say DEF Which is shorthand for define or definition and we're going to give it a name and then we have Those little parentheses if we forget those we're gonna have a bad time We need the parentheses and then in there we would define any parameters We're gonna keep this first one very basic and there'll be no parameters But we still need the parentheses in there and then colon and hit enter and notice how Most IDEs will do this automatically where they will drop you down a line and indentate you automatically Then we can just fill in our code and we can have pretty much anything we want in here I'm just gonna put a print statement just for testing and we're just gonna say this is a function Now we can work with this thing as needed Okay, this last one was a little bit simplistic. Let's ramp up the difficulty here So we're going to define a function with parameters and return a value In the last video we made a paint calculator which Calculated based off the square footage how much paint we need we could have very easily used functions in that and most programs would have So we're gonna make a function called sqft which shorthand for square footage parentheses and let's define some parameters. I'm gonna say w for width and h for height Now we can add our programming logic in here. So the value We're going to return is going to be simply the width times the height Now we're going to use the return keyword To return that value Notice how these are both very simple, but they're fundamentally very different This first one basically has no parameters and run some code and once it's done. It just jumps right back out of here This one, however, we have to give it two parameters and there's gonna be some logic and it's going to return a value It's up to us to decide if we want to actually work with the value that's returned It's not actually mandatory. We can just ignore it if we wanted to But this is what I mean by functions can get very complex very very quickly We have two very small functions that act completely differently Let's take a look at how this actually works. How would we call a function? Now, what do we mean by call a function? Remember this code is not going to execute until we Specifically tell Python to run it. Let's go ahead and run our program as is and see what happens absolutely nothing So what it's doing is it's reading from the top down It's saying define test define sqft and it has this queued up in memory ready to go But it's not actually going to use it until we tell it to So let's tell it to run it Most editors are going to be smart enough to tell you. Hey, this is an actual function and in VS code This little block means it's a function and notice the block because it's a block of code Now if I just hit okay or enter or click on it, it does nothing This actually will not run. We have to add those parentheses Now suddenly see how it says def test and it knows we're trying to call that function Let's go ahead and run and ta-da. This is a function So Python is reading this from the top down defining tests defining sqft Getting here and saying oh it wants to actually call this There are no parameters. So we don't have to supply any arguments You may be wondering what the big deal here is. Why would we even mess around with functions? I mean, we could have just printed this out Well, let's take a look at a specific situation. Let's say we wanted to call that function multiple times We're say 4x in range And we're gonna say range four. So we want to call that multiple times Now we could just write this out like this four times. You could do something like this And you'll see people do that however, that's not the best way What if your boss comes and says well, I want you to do that 20,000 times Okay, you're gonna have a very big file calling that out. It's a much simpler way You would say 4x in range and then you would call this so what we've done now is we've Created a loop and we have a lower boundary and an upper boundary and it's going to call it So we have not created an infinite loop. It will call our function. Let's see this in action and ta-da There it goes right there So that's the power of a function You can define a block of code and then decide when and how it runs Now in typical fashion with these videos, I do like to ramp the difficulty up here So we're going to call a function with parameters And this is what I mean by this is gonna get confusing fast Remember our little buddy the sqft function, which we define which we haven't really worked with yet Let's go ahead and say x equals sqft Now we have some parameters and it's smart enough to know that it's a param So we have to feed it some information now. I'm gonna say 12 by 8 Bonus question here. These numbers are these parameters or these arguments? Well, they're arguments So these up here are the parameters. They're in the function definition The values we're feeding it are arguments and you're gonna get people that will go pun intended Argue this all day long because people get it so confused All right, let's go ahead and print out the square footage So the square footage is 96 Now you see just how powerful this is You can define some sort of logic and then call it on demand when you need it and get the value back from it and use it accordingly Just a quick recap what we've talked about in this video is well functions are amazing They're also very complex it reads from the top down You have to use the DEF or the DEF keyword to define a function Functions can have zero or more parameters those parameters have to be fed arguments. I Hope you enjoyed this video You can find the source code out on github.com if you need additional help myself and thousands of other developers are hanging out in The void realms Facebook group This is a large group with lots of developers and we talk about everything technology related Not just the technology that you just watched and if you want official training, I do develop courses out on you to me calm This is official classroom style training if you go out there and the course you're looking for is just simply not there drop Me a note. I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always Help me help you smash that like and subscribe button the more popular these videos become the more I'll create and publish out on YouTube. Thank you for watching