hey guys welcome to the next blog on Python tutorial for beginners in this blog I'm going to show you what is a function in Python and how to use functions in Python so first of all what is a function so a function is a group of statements within a program that performs a specific task now functions can be of two types one is built-in function and other is user-defined function built-in function we have already seen that print is a built-in function or for example input is a built-in function or for example min is a built-in function and so on now usually function does one task at a time so you can see this print function only prints something whatever input you give here is going to print it input function takes some input from the user main function finds out the minimum out of some values so a particular function do one task at a time now let's see how we can define a function so to define a function you use a keyword de F and then you give the name of the function so name of function and after the name of the function you give these parentheses and you provide number of arguments or parameters so for example Arg 1 comma Arg 2 comma Arg 3 and so on so you can give any number of arguments to your function now after this ending parenthesis you give this colon and then under this function signature you write some statements which you want to execute when this function is called so for example if you want to print something you can print something or if you want to calculate something for example the product of two or three variables or a sum of two or three variables you can do under this function declaration so let us take an example sample of function and let's see how we can define real-life function so I'm going to define a very simple function which is going to add two values so I'm going to name it as sum and for example it takes two values one is let's say Arg 1 and other is Arg 2 and then after the colon I want to add these two values so I can just write print here and then I can write R 1 plus R 2 so this is a very simple function which takes two arguments and then add these two arguments and print them so this is how you declare a user-defined function now after declaring a function you need to also call this function so in order to call the function you just use the name of the function and then you provide the arguments which is required by the function so our function requires two argument right now R 1 and R 2 so we are going to provide these two values let's say I want to provide 15 as the argument 1 and I will provide 60 as the second value now let's run the code and let's see what happens so when we run the code you can see our function prints 75 which is the sum of these two values which we have provided as an argument to this function now also if you remember this plus operator you can also use to concatenate two strings so I can use this sum function and this time I'm going to provide for example hello as the first parameter and then world as the second parameter and then I'm going to run the program and it's going to print hello world in addition we can provide to float numbers here for example I will provide 15 point 6 4 7 and the second argument I'm going to provide is 80 point 2 5 8 and this is also allowed so I'm going to just run this code and it gives us the sum of these two values so this function sum is doing one single task which is to add two values whether it's a string or it's a number or it's a float value now you may also observe that when I provide for example as a first argument I will provide a string and as a second argument I will provide a number here will this work let's see so it's going to give us an error and this error says can't convert into object to string implicitly so this is a problem so to solve this problem we can provide here a simple condition and we are going to check the type of both the arguments so if type Arg 1 is not equal to type or 2 this should be Arg of 1 not easier 1 so let me just change this here and if the type of art 1 is not equal to R 2 we are going to just use this keyword which is returned so this return keyword is used to return something so you can return for example 0 here or any string here and when you write this return function without any value here it's going to return nothing but as soon as this return keyword is called nothing after that will be executed so even though you are returning nothing from here then also this statement will not be executed so whenever return is executed after that all the statements which are under the return will not be executed also we can print a message here that please give the odds of same type okay and let's run the code now and you can see now it prints this message which says please give arcs or same type so if these arguments are not of same type whether it's integer or a string or a float value if the user provides first argument which is a string type and the second argument which is integer type then this condition will be true and this statement will be executed which is going to print this message and then return is called and after this whatever statements are there will not be executed so now as I said you can also return some values from a function and here when you don't give any values after this return keyword it's good not going to return anything but let's return the addition of two arguments using our return keyword so I'm going to use this return keyword here which is going to return the addition of these two values using this sum function so now what will happen is let's run the code once again and you can see this sum is executed this sum is executed this sum is executed but the result is not printed so in order to get the result out of this function when it returns something we need to save this return value in a variable so let's save this value into a variable and then you can use this variable to print the value of the sum so I can do something like this also you can directly and close this sum function inside a print function and then also it's going to print the sum of these two strings so either you can assign the result of this sum function which is returning the result into a variable or you can use directly this print function to get the result and print it so I'm going to use print with other two functions also so first print and the second print here and let's run the code once again and now you can see what happens so first of all it prints 75 which is the addition of these two numbers which is returned by this sum function into a variable a and we are printing the sum using this print function here in the next result it prints hello world because this sum function now returns the concatenation of these two strings similarly this sum function is going to return the sum of these two float values and in the last result because the type of Hello is not equal to the type of 15 then this message is printed because this condition is true in the last case and then return will be called and when this return is called we were printing whatever is returned from the sum function and you can see at last none is printed because at last this sum function is returning nothing because the type of these two arguments is not same so we were returning without any value and that's why none is printed here now there are few things to note here okay so the first thing is you can define a function using DEF keyword and then the name of the function and under these parentheses you provide the arguments so these two are called arguments so this is argument 1 and this is an argument 2 also you can use alternatively the name parameters for these two arguments so this is a parameter 1 and this is a parameter to now it's possible to return from a function like you can return the addition of two values or it's also possible to return nothing from a function so when you use return without a value it's not going to return anything now when you use this function somewhere it's called calling a function now at last let's discuss about some of the benefits of using functions so the first benefit is function makes your code simpler because if you don't use function to execute this kind of code then you need to write this code again and again whenever you want to use this functionality at different places the second advantage is function makes your code reusable so the same code is used to add two integer values to concatenate two string to add two float values and it's also used to give the error if you provide the arguments of different types so that means you write the code once and use it multiple times and that results in faster development of the code so if you use a function you can develop your code much faster than if you don't use our function and the last but not the least advantage is when you declare functions you can test and debug your code in a better way so this is how you can declare and use functions in Python I hope you enjoyed this blog and I will see you in the next blog
Post a Comment