Functions in Python are easy πŸ“ž

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody today I need to explain functions think of a function as a block of reusable code to invoke a function you place a set of parentheses after the function name to invoke it here's a scenario I need to sing Happy Birthday three times I know it's a weird example but it makes a lot of sense just trust me on this if I need to sing Happy Birthday three times I would write something like this I'm going to create my own version of the Happy Birthday song thank you this is one verse if I need to repeat this code three times without using functions I could either repeat this code or maybe place it within a loop so here's my Happy Birthday song three times but there's a better way of handling this that doesn't involve repeating our code or using Loops what if I could write this code once then reuse it whenever I need to that's where functions come in to define a function you would type def then a unique function name let's name this function the happy birthday function add a set of parentheses a colon any code that belongs to the function you'll want to indent underneath then to invoke this function I would type the name of the function happy birthday add a set of parentheses and that's it when you invoke this function you will execute this code once if I need to execute this code three times I would just call it two more times happy birthday happy birthday happy birthday to invoke a function you type the function name then add a set of parentheses I like to think of the parentheses as a pair of telephones talking to each other you call a function to invoke it hey happy birthday function execute your code with functions you are able to send data directly to a function using what are known as arguments you can send values or variables directly to a function place any data within the set of parentheses I'll send my function a first name any data you send a function are known as arguments but you need a matching set of parameters that are in order what exactly is the data we're sending in well it's a name I will add one parameter to my happy birthday function I will name this data name a parameter is kind of like a temporary variable that's used within a function I'm going to replace this instance of U with a name I will use an F string replace you with the placeholder I will add my parameter name so happy birthday to bro we could pass in some other names what about Steve and Joe here we are happy birthday to bro happy birthday to Steve happy birthday to Joe when you invoke a function you can send more than one argument let's send an age this time I'll send 20 30. and 40. but when I run this we have an error we're passing in two arguments but our function is set up only to take one I would need a matching number of arguments to invoke this function we will need two parameters we have a name and we have an age then let's use this age you are let's make this line enough string age years old let's try that again happy birthday to bro you are 20 years old happy birthday to Steve you are 30 years old happy birthday to Joe you are 40 years old when you invoke a function you can pass in some data those are known as arguments but you'll need a matching set of parameters the order does matter let's see what happens when I switch these two parameters age then name happy birthday 220 you are bro years old happy birthday 230 you are Steve years old happy birthday 240 you are Joe years old so the position of the parameters does matter same thing goes with the arguments you also could name these parameters something unique maybe X and Y happy birthday to X you are y years old that's also valid let's try another example I'm going to create a function to display an invoice there will be three parameters a username and amount and a due date let's print hello I should make this an F string username we'll use another F string your bill of amount let's precede this placeholder with the unit of currency I will also add a format specifier 0.2 f is do on our due date whatever that parameter is to invoke this function we will type the function's name add a set of parentheses a username and a mount and a due date let's make up some username and amount I guess 42.50 I'm just making up a number here then a due date the first of January I guess here is my invoice hello bro code here bill of 42.50 is due on January 1st let's change these arguments Joe schmoe is the username he owes 100 and one penny due on the first of February or January 2nd depending on how you read dates in your region hello Joe schmoe your bill of 100 and one cent is due on one slash two that's another example now we need to explain the return statement return is a statement that is used to end a function and send a result back to the caller here's an example we have a variable z z will equal will invoke a function to add two numbers together such as the numbers one and two when we invoke a function we can send some data back after adding one and two we will send the result which would be three then this value can be assigned to a variable then we can print whatever Z is so let's create some functions let's create a function to add two numbers together the parameters will be X and Y let's say Z equals X Plus y then we will return our value Z so I'm not going to print Z directly right now let's subtract X and Y subtract Z equals x minus y return Z multiply times y then divide x divided by y return Z let's invoke our add function pass in two numbers one and two then I'm going to print the result after adding these two numbers together the result is three what about subtract subtract one and two the result is negative one multiply the result is 2. then divide 1 divided by 2 is 0.5 after we resolve this function a value is returned just imagine that after we finish this function this function becomes whatever is returned three this function becomes negative one this function becomes two this function becomes 0.5 let's write something a little more complex we will create a function to create a full name create name we'll need two parameters for a first name and a last name I'll name these first and last what do we want to do within this function let's capitalize the user's first name first equals first dot capitalize method then do the same thing with the last name last equals last dot capitalize then I'm going to return the user's first name plus their last name then I'll add a space in between their first and last name this is also valid outside of the function let's create a full name variable that invoke the create name function so this function is going to capitalize the first and last name for us I'll type in my first name all lowercase same thing with my last name then let's print our full name and here is my full name variable we sent our function some arguments we have some parameters set up we took those values made them uppercase then concatenated these strings together then returned them as a single string let's try this with a different name SpongeBob SquarePants SpongeBob now has a full name the first and last names are now capitalized using the return statement you can return some data back to the place in which you call a function well everybody that's a function it's a section of reusable code to call a function you type the function's name add a set of parentheses you can send a function some data which are known as arguments but you'll need a matching set of parameters you also do have the option of returning some data back to the place in which you invoke a function we'll be using functions a lot in the future but we will get more practice with them and those are functions in Python
Info
Channel: Bro Code
Views: 416,802
Rating: undefined out of 5
Keywords: python tutorial for beginners, python course
Id: 89cGQjB5R4M
Channel Id: undefined
Length: 10min 38sec (638 seconds)
Published: Sat Nov 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.