Python Tutorial for Beginners 13 - Defining Functions


hello guys welcome once again in this blog I am going to show you what is a function how to use a function and why we use function in Python so first of all what is a function so let's see what is a definition of function a function is a group of statement that together performs a specific task so basically we write a function to do a particular task again and again without rewriting the same code again and again and functions make your code readable and you know maintainable maintainable means if you want to you know change something in your code by writing functions it's easy to maintain your code okay so always remember whenever you want to write a specific logic or code again and again or more than once use a function instead okay so let's see how we can define a function so in Python you can define a function like this you need to use our keyword which is def and then you give a space and give a function name and then in the brackets you give a something called arguments okay so whatever you give in the brackets these are called arguments and there is no limits to arguments arguments you can define any number of argument which you want to pass to a function and then a semicolon and then comes the statement statements are the piece of code which you write inside a function now suppose I want to make a function to print a hello to some name so either I can do print as we have been seeing that I can use print and I can say hello hello and the name for example I want to say hello mark right so and I can print it but for example I have 25 names or hundred names which I want to say hello and I would don't want to rewrite this print again and again by the way this print is also a function and we are passing the string as an argument to this print but print is a built-in function which I have shown you in the previous blog what our built-in function and how to use built-in function now let's make our function and what function we define are called user-defined function so we will define a function for example call we will call our function def and then name our name of our function will be hello for example okay and what we are going to we are going to pass a variable so you can name your variable or any name you can give any name to your variable for example name or X or Y or whatever you want to give an argument you can name your argument depend of depending upon your wish so I will name my argument as name because it's more logical because I want to print name right and then I give this : okay now after this : inside the function we write statement so what we want to do we want to print hello and the name whatever we pass in a function right so we will say hello print and then we pass this argument whatever argument will you will pass we will pass this argument to the free print function and then what we want that we want to print hallo and whatever name we will pass ok now I will just run this function or how we can use this function so function is defined now ok now how we can use this function we can just call this function hello and then we can pass a name which is a string right so we want to pass a function which takes our string ok so for example I say mark ok and I press ENTER and it says hello mark ok and once again I can just once again called hello and this time I pass a different name for example John and save hello John and in a similar way you can pass any name it will print other names also so what this function has done for us it makes our code really you know readable because we are not Rita writing this piece of code which is print for example suppose you want to perform you know 10 or 20 line of code in one go and this these 20 line of food perform a particular logic and you don't want to you know Ryrie write these 20 line of code again and again so you can use a function instead to enclose these 20 line of code and then you can use this function so that you don't have to write this 20 line of code again again again and again you can just call a function by its name if it takes an argument you can pass the argument and then it will give you the result right now it's not necessary that you always has to pass any argument you can define a function without any argument also right so always remember a function can have zero argument or any number of arguments minimum is 0 or no argument and maximum is depending upon you okay now there is a yo and one more thing I want to say here is function don't take only strings they can take numbers so let's make a little bit useful function to add two numbers so I will just define a function by d EF and I will name my function as ad function okay and this will take two argument for example x and y so I will just separate my arguments by a comma and then I will give a give this poll in here and then I want to print the result okay so what I can say is I can return and return means giving the answer okay simply giving the answer and what I want to return I want to return the addition of X plus y okay and now I will press ENTER two times that means my function is defined now I can use this function to add two values so I will call this function add and I want to add for example 100 and 500 okay and what will what this will do it will add 100 and 500 and it will return the answer so if when it returns down so you can save your answer in some other variables for example I can define a variable called sum and I can save this sum in this variable okay so for example I define a variable called sum 1 is equal to add 100 comma 500 and I can press ENTER now my sum is in my some one variable so I can print by someone like this someone it gives 600 what this function has done for us add function it has taken x and y so X is assigned or 100 is assigned to X and 500 is assigned to Y and then it adds the value of x and y which is 100 plus 500 and it gives the result return the result and when a function returned any result you can save it into another variable okay so it has saved this result into the sum variable and it returns and same thing you can do again and again so for example you want to add some other numbers for example 234 and 3 3 3 you can do it like this and you can call some to add give will give you the result okay so in this way you can use functions in Python I hope you enjoyed this blog

Post a Comment

أحدث أقدم

Recent in Technology News