Functions | Python | Tutorial 14

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you guys about using functions in Python now a function is basically just a collection of code which performs a specific task so I can take a bunch of lines of code that are basically doing one thing I could put them inside of a function and then when I wanted to do that task or do that one thing that the function was doing I can just call the function and so functions really help you to organize your code a lot better they allow you to kind of break up your code into different you know little chunks that are doing different things and they're really just awesome so functions are like a very core concept when we're talking about programming in Python so I'm gonna show you guys how to create an awesome function today let's say for the purposes of this tutorial we want to create a function that says hi to the user so the one task that our function performs is basically just saying hi to whoever is writing the program so over here if I want to write a function the first thing I have to use is a keyword in Python it's called def so d EF and basically when Python sees this keyword it's gonna say okay this person wants to use a function so after we type out def we need to give this function a name so just like when we are creating variables we give them descriptive names we also want to do the same thing with functions so if I'm creating a function I can just give it a specific name which basically says like what it's doing so we're gonna create a function that says hi so I'm just gonna call this function say hi just like that and once we type out the name of the function and so also I could type out say hi with no underscore or I can type out say hi with an underscore both are considered like good practices and Python a lot of times if you just have a simple like two word function like this you don't need to put an underscore but we could put an underscore there if we wanted after we type out the name of the function I'm just gonna type into open and close parentheses and then I'm gonna type a colon and basically what this is doing saying to Python like alright all the code that comes after this line is gonna be inside of our function and in order to write code that's gonna end up being inside the function we actually have to indent it so over here you'll notice that when I clicked enter my text editor automatically indented the text so it's automatically like using this indent here and that's kind of like one of the rules in Python is like the code that goes inside of this function needs to be indented so if I was to write some code like out here this is no longer gonna be considered inside the function so you can see as I type out code and obviously this isn't real code but as I type text over here that's indented this little like marker over here it's basically saying like oh yeah that's inside the function but then when I write code over here that is like not at the same indentation level as this stuff it's no longer considering it inside the function so that's just a little thing any code inside this function needs to be indented alright so our function is just gonna say hi to the user so I'm just gonna have it print out some text it's gonna say hello user so this is a very simple function obviously we just have one line of code and inside of a function you could have you know as many lines of code as you want but for our purposes we only need one line in order to perform our function so now all we have to do is call this function so if I want to execute the code inside of this function I have to do something called calling it so if I was to just run my program as it is right now I'm just gonna run it you'll see that nothing happens over here right even though this function is printing out hello user when I run the program it's not doing it and that's because of the code inside of a function isn't gonna get executed by default the code inside of a function is only going to get executed when we specify that we want to execute it and in order to do that we're gonna have to do something called calling the function so in order to call the function you basically just type out the functions name and those open and close parentheses so I'm just gonna type out say hi and open and close parentheses and now when we when we run this program again you'll see that it prints out hello user so we're executing the code inside of the function and I want to show you guys just one more thing here just talking to you guys about how these functions actually work so up here I'm gonna print top and then down here I'm gonna print bottom so I want to show you guys the flow of these functions inside the program so when I run this program you'll see we print out top hello user and then bottom so essentially what's happening is when python goes through and execute this program it goes over here to this first line it says okay we want to print out the word top and then it goes down here and it says okay we want to execute the say hi' function so python actually jumps up and it goes over to this say hi' function and it's gonna execute all of the code inside of this function so it's gonna go through execute all this code and then once it's done executing all the code in the function it's gonna jump back down here and it's gonna move on to the next line which is bottom so that's kind of like the flow of functions again with functions generally when we're naming these functions you want them to be named in all lowercase and usually when we're naming stuff in Python if there's two or more words we're gonna use an under space or an underscore in between them so I could write this out as say underscore high but in a lot of situations though if I have a function like this where the name is really short I might just be easier to leave it without an underscore but why are we just put an underscore in there just to be super Python official alright so now we can actually make these functions a little bit more powerful and what we can do is we can give them information so a lot of times when we write a function we're gonna want to have additional information that gets passed in and these are called parameters so a parameter is a piece of information that we give to the function so over here I can actually specified that this function needs to receive some parameters so I can basically say like hey if you're gonna call this function you need to give us some information you need to give us some parameters and all I have to do to do that is just type out the name of the parameter that I want to receive so why don't we allow the code calling this function to tell it what name to say hi - up here I can just say name and basically what this means is it means whenever I call this say hi function we have to give it a name so down here if I was to call this I have to include a name in here so I can say like Mike and what we can do now is we can actually access this parameter or this variable inside of our function so I could come over here and instead of saying hello user I could say hello name and basically what this is gonna do is it's just gonna say hello to whatever name got passed in here so I'm actually gonna copy this and we will do this twice so I'll say hello Mike hello Steve and now when I run this program you'll see that instead of just saying hello user it's saying hello to whichever name I passed into the function so that's why this can be really useful right we can give the function information and depending on the information we give it it'll perform its task a little bit differently I can also include more than one parameter so you can I mean technically you could have like as many parameters as you want so I can put another one in here we could say age and now I'm gonna have to pass in an age along with these so I'm just gonna pass in age and pass in an age for down here and I'm just gonna pass in strings so we can say hello name you are age so I'm passing in two pieces of information and now when we run this program it's gonna call the function and it's gonna use both of those pieces of information so say hello Mike you are 35 hello Steve you are 70 so essentially we're writing out this one line of code which just prints out like Hello to someone and we're allowing this function to receive two parameters so the name and the age and depending on the name and the age the function is gonna print out hello a little bit differently and that's kind of the beauty of using functions so you could pass anything you want into a function so for example I could pass in a integer instead of a string for the age so like I could pass in a number the only difference is over here we're gonna have to convert this into a string but it's gonna work just the same so you can pass in strings numbers boolean's arrays you can really pass any type of data into a function and it's gonna work so you can see here we get the same result so that's the basics of functions and as you go through with Python you're gonna you know be using functions more and more and generally it's a good idea to break your code up into different functions so whenever you have like a grouping of code that's designed to perform a specific task that's usually a good candidate to be put inside of a function hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we released new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying chopped Academy and you want to help us grow head over to draft Kadim EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 24,148
Rating: 4.984334 out of 5
Keywords: Programming
Id: LbOwv6y6xjo
Channel Id: undefined
Length: 10min 15sec (615 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.