A function in python is a group of statements within a program that performs a specific task. Usually functions input data, process it, and “return” a result. Once a function is written, it can be used repeatedly.
We’ll be covering the following topics in this tutorial:
Types of Functions
Functions can be of two types one is a built-in function, and other is a user-defined 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. you can see
print() function only prints something whatever input you give is going to print it.
input() function takes some input from the user.
min() function finds the minimum out of some values.
So a particular function do one task at a time.
Define a function
def nameoffunction(arg1, arg2, arg3): print("")
To define a function, you use a keyword def, and then you give the name of the function, and after the name of the function you give these parentheses
(), and you provide the number of arguments or parameters. You can give any number of arguments to your function. 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. For example, 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.
Calling of function
I’m going to define a very simple function. Which is going to add two values. I’m going to name it as the sum and for example
def sum(args1, args2): print(args1 + args2) sum(10, 10) Output: 20
It takes two values one is let’s say Args1, and other is Args2 and then after the
colon (:). I want to add these two values. I can write print and then I can write args1 + args2. It is a very simple function which takes two arguments and then adds these two arguments and print them.
This is how you declare a user-defined function. Now after declaring a function. You also need to call this function so in order to call the function you use the name of the function, and then you provide the arguments. Which is required by the function. Our function requires two-argument args1 and args2. we are going to give these two values.
Let’s say I want to provide 10 as the argument-1, and I will provide 10 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 20 which is the sum of these two values, which we have provided as an argument to this function.
Now also if you remember this
'+' operator. You can also use to concatenate two strings. 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.
def sum(args1, args2): print(args1 + args2) sum('Hello ', 'World') Output: Hello World
in addition we can provide to float numbers for example I will provide
15.647 and the second argument I’m going to provide is
80.258 and this is also allowed. I’m going to just run this code and it gives us the sum of these two values.
def sum(args1, args2): print(args1 + args2) sum(15.647, 80.258) Output: 95.905
Above function, the sum is doing one task: to add two values, whether it’s a string or a number, or a float value.
You may also observe that when I provide a string as a first argument, and I will give a number as a second argument. Will this work.
def sum(args1, args2): print(args1 + args2) sum('Hello ', 80)
Let’s see so it’s going to give us an error and this error says
Can't convert 'int' object to str implicitly. This is a problem. To solve this problem, we can provide a simple condition, and we are going to check the type of both the arguments.
def sum(args1, args2): if type(args1) != type(args2): print("Please give the args of same type") return print(args1 + args2) sum('Hello ', 80)
If the type of
args1 is not equal to
args2. We are going to use
'!=' keyword, which is returned.
Let’s run the code, and you can see. It prints message which says, please give args of the same type. Suppose these arguments are not of the same type. Whether it’s an integer or a string or a float value, if the user provides the first argument, which is a string type and the second argument is an integer type, this condition will be true. The statement will be executed. It will print the message and then return is called, and after this whatever statements are there will not be executed.
You can also return some values from a function. When you don’t give any values after the return keyword, it’s good not to return anything but let’s return the addition of two arguments using our return keyword. I’m going to use the return keyword, which will return these two values using this sum function. Now what will happen is let’s run the code once again, and you can see the sum is executed, but the result is not printed.
def sum(args1, args2): if type(args1) != type(args2): print("Please give the args of same type") return(args1 + args2)
To get the result of the above function. When it returns something, we need to save the return value in a variable. Let’s save this value into a variable, and then you can use the variable to print the value of the sum.
def sum(args1, args2): if type(args1) != type(args2): print("Please give the args of same type") return(args1 + args2) s = sum(10, 80) print(s) print(sum(10, 80))
You can directly and close this sum function inside a print function, and it’s going to print the sum of these two strings. Either you can assign the result of the sum function. Return the result into a variable or use the print function directly to get the result and print it.
Benefits of using functions in Python
• The function makes your code simpler because if you don’t use the function to execute the code, you need to write the code repeatedly whenever you want to use the functionality at different places.
• The function makes your code reusable. 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, you write the code once and use it multiple times, which results in faster development of the code. If you use a function. You can develop your code much faster than if you don’t use the function.
• when you declare a function you can test and debug your code in a better way.