Python Functions (The Only Guide You'll Need) #12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys i'm punit from programmers and welcome back to this series on python in the last few videos we learned about decision making and loops we will start a new section today we have been using the term function a few times in our videos without explaining what they are in this video we will look into functions in detail and learn how to create them and why they are used so let's get started in programming a function is a group of related statements that performs a specific task they help us divide a large program into smaller chunks so that it's easier to understand and change suppose you need to create a program that draws circles and rectangles of different colors based on user input instead of creating one big chunk of statements to create this program we can create three functions to solve this first create a circle function then a rectangle function and then the color the shape function this helps us to divide complexity and we can focus on only a small part of the problem at one time now let's see how we can create a function in python to create a function we use the def keyword which stands for the function definition followed by the function name then i'll use the empty parenthesis followed by a column so i'll say def def greet empty parenthesis and column here we have defined a function name greet however this code will give us an error because the function body is missing let's fix that for now i'll only add two print statements as its body so i'll say print hello and then let me add another print statement print how do you do remember we need to use indentation to specify that this is the body of the function we have successfully created a function named greet now let me run this code and we don't really see anything this is because defining a function won't do anything in itself to bring the function into action we need to call it our function name is greet with empty parenthesis so to call it i will just use greet with empty parentheses so here i'll say greet and now when i press run then you can see that hello and how do we do are printed here's how this code works when we call the function the control of the program jumps to the function header then the statements inside the function are executed when this task is completed the control of the program goes back to the function call and the next statement after the function call is executed one thing with functions is that once we define a function we can call it any number of times let's call our greet function three times so here i'll say greet and one more greet and now if i press run as you can see the function has run three times that's why these two statements were executed one two and three times so if we need to perform a task again and again we can wrap the codes inside a function and use the function any number of times we just need to call the function again and again one thing to remember when we create a function is that we need to define a function first before we can call it this code for instance will not work let me run this code to show you here when the grade function is called python doesn't know that this function exists because it's defined after the function call so always remember to define a function before you use it before moving to the next section of the video i'd like to mention that the program is team has created an app that allows you to learn python from your phone the app contains bit size lessons that are easy to understand a built-in interpreter so that you can run python on your phone quizzes and many more features the app is available on both ios and android the links are in the video description below now let's talk about python arguments and how our greet function from the previous example can be changed to allow arguments suppose we want to make our greet function a bit more personal so instead of printing hello we want to print something like hello jack or whatever the person's name is in such cases we can pass values to a function these values we pass to a function are called arguments so i'll go back to the code that i started with i'll remove these two greet functions because i want only one and here inside the grid function i'll pass the jack string like this this value that we passed during a function call is called an argument so jack here is an argument and in the function definition i'll add a variable in the greet function i'll call it name this name variable accepts whatever value is sent as an argument during the function call in this case jack is transferred to the variable name now i can use the name parameter inside the function so now i can say hello comma name and when i press run then i get hello jack let's see step by step how this function works when we call the greet function with the argument jack then this is passed to the name variable inside the function definition then the statements inside the function are executed we can use the name parameter inside the body of the function when this task is completed the control of the program comes back to the place from where the function was called and the next statement is executed in this case there's nothing here it's also possible to pass multiple arguments to a function as per our needs if we need to pass multiple arguments to a function we can separate them by commas let's see this in action by creating a function to add two numbers so i'll remove the old code and i'll create a new function called add numbers so i'll say def add underscore numbers obviously i'll need two parameters n1 and n2 and then inside the function i'll say result equals n1 plus n2 print the sum is and here i'll say result now outside the function i'll say number 1 equals 5.4 number two equals six point seven and then i can call add underscore numbers number one comma number two in this program we have passed number one and number two as arguments to the add numbers function these arguments will be accepted as n1 and n2 once they are passed to the add numbers function so inside the function n1 will be 5.4 and n2 will be 6.7 then we have added the numbers and printed them inside the function itself let's run this code and see the output as you can see i now have the sum of those two numbers in our program to add two numbers we are finding the sum of numbers and printing it it's working fine however it's better just to find the sum inside the function and print the result somewhere else we can achieve that by using the return statement inside the function i'll remove this print statement and i'll say return result when we return a value it comes back to the function and we can assign this return value to a variable like this so here i can say result equals add numbers number one comma number two and i can print the result as print the sum is and then i can say result let's see step by step how this program works we call the add numbers function with two arguments number one and number two which are accepted by the function definition as n1 and n2 the sum of n1 and n2 is calculated and the result is returned to the function call this return value is assigned to the result variable and in the next line we just print the result variable outside the add numbers function whenever a return statement is encountered inside a function the control of the program goes back to the place from where it's called let's see an example of this let's get back to our grid function that we wrote before i'm pasting it here when i press run then we get hello jack and how do you do here now let me modify this program a little bit i'll add a return statement after this line and let's see what happens now when i press run then you can see that only hello jack is printed this is because when the return statement is encountered immediately the function terminates and control of the program goes back to the place from where the function is called at this point we have covered all the basics to create a function these functions we created ourselves are known as user defined functions actually we have already used functions numerous times in all of our videos remember print it is also a function the function definition of this print statement is defined somewhere inside the python programming language that's why we can just call the function and it just works these functions defined inside of python are called built-in functions we have also used other built-in functions like float int input and so on in our videos there are numerous built-in functions available in python that make our life a whole lot easier for example suppose we have a list like this now if we need to find the length of this list we can use the len function so here i can say length equals len max and i can print the length of the list as print length is comma length let's run this code and see the output when i press run then as you can see it says length is 5 which is the length of the number of items in this marks list if we instead needed to find the sum of the items of the list we don't need to manually write the code ourselves we can use the sum function that's available in python so here i can say marks underscore sum equals sum of marks and then i can print the sum as print the total marks you got is and then i can say marks underscore sum now when i press run then it says the length is 5 and the total marks you got is 308 which is the sum of these marks if you're interested you can find all the built-in functions available in python in the programming.com website the link will be in the description below let's put all the things we have learnt in this video in action suppose you have just attended a university examination the marks you obtained in various subjects are stored in a list like this you want to find the average marks you obtained in the exam and based on the average marks you want to find your grade the grading rule is like this you will get grade a if the average marks is equal to or above 80 you will get grade b if the average max is equal to or above 60 and less than 80 you will get grade c if the average max is equal to or above 50 and less than 60 and if the average marks is less than 50 you will get great f to solve this problem we will create two functions one to find the average marks and another to compute the grade so let's get started i'll start with the function definition but before that let me add a little comment i'll say function to find average marks now let me define my function as def fine average marks now the argument to this will be a list of marks and inside i'll say sum of marks equals some marks i also need to find the number of subjects or the number of marks i have so i'll say total underscore subjects equals the length of this marks list and then now i can calculate the average as average underscore marks equals sum of marks divided by total subjects let me return this average marks variable and now outside the function i can say average marks equals find average marks i'll pass in the marks list and here i'll say print your average marks is and i'll say average underscore marks when i press the run button i can see that my average marks is 67.8 which is the average of these five numbers which is calculated as sum of these numbers divided by the total number of subjects which is 5 in this case now we need to create another function to calculate the grade based on the average marks let's do that now i'll create another function but before that let me add a little comment i'll say calculate the grade and return it and then i'll say def compute underscore grade now the parameter or the argument to this will be the average marks so i'll say average marks and inside i'll say if average marks greater than equals 80 then grade is a alif average marks greater equals 60 then the grade is b alif average max greater than equals 50 in this case the grade is c and the else clause i'll say grade equals f and outside the if block i'll say return grade now i can use this compute grade function as grade equals compute underscore grade average marks now let me print this grade as your grade is and the grade variable now when i press it on i can see that my average marks is 67.8 and my grade is b which is not the best result but at least i now know what my grade is before we end this video here's a programming task for you can you create a program to add and multiply two numbers for this create two functions add underscore numbers and multiply underscore numbers these functions should compute the result and return them to the function call and the results should be printed from outside the function you'll find the answer to this question in our github repository also visit our website programmies.com for more tutorials and examples the links will be in the description below now let's recap what we learned a function is a block of code that performs a specific task we use the def keyword to define a function to bring the function into action we need to call the function we can call the same function as many times as we want as per our needs we can pass values to a function these values passed to functions are called arguments or parameters the return statement can be used inside a function the return statement returns a value from a function and exits the function as well that's it for this video i hope you learned something if you're just watching the video without writing any code i highly encourage you to try the programs in this video on your own the only way you can be a good programmer is by trying by the way you can find all the programs from this video on github i've provided the link in the description below feel free to copy the program and edit them as you please and if you have any questions and feedback use the comment section below in the next video we will learn about different types of function arguments in python join me in this video series and let's explore the exciting world of programming together if you like this video hit the like button now and also don't forget to subscribe to our channel and ring that bell icon so that you don't miss the next video thanks for watching and i'll see you in the next one happy programming
Info
Channel: Programiz
Views: 66,091
Rating: 4.9644971 out of 5
Keywords: python, programiz, python functions, learn python, functions in python
Id: -Bkupx9gX0o
Channel Id: undefined
Length: 17min 19sec (1039 seconds)
Published: Wed Sep 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.