Functions are fundamental feature of Python programming language. Functions provides reusability of code parts. Functions provides some abstraction from code. We can define some code block and use it with a single line without copy and pasting the whole code block.In this tutorial we will look how to define and use Python functions or methods.
Define Function with Def
def keyword is used to identify function start in python. After the
def keyword we provide the function name and parameters. Parameters are provided in brackets
( .. ) . Parameters are separated with commas
, . Parameters are optional and if we do not need them we can omit them. Functions definition ends with double dot
After the first line we provide function body or code block. Function body is indented to specify the body area. Here is the syntax of the function definition.
def FUNCTION_NAME(PARAMETER1, PARAMETER2, ...):
Now we will make an example to learn functions. We will create a functions where it has no parameter and only single line of code in its body or code block. The functions name will be
print("I am working boss")
This is a simple for python but important for us.
Calling functions are easier than creating them. We will just provide the name of the function and add brackets end of this name. If we need to provide parameters we can put them into brackets.
In this example we will call the function,
myprint , we have previously created. As it has no parameter we do not put anything into brackets.
Functions With Parameters
What makes functions strong is parameters. We can provide any number of parameters in any data type. Now create an example which have two parameters named
b . Parameters type are integer. This function named is
sum . This function will sum up given parameters.
In previous example we have called the function and print some text to the output. Sometimes this is not we want. We may need to get or return some data but not put into output. We can use
return in order to return data like string, integer, object, list etc. from functions. We will just put
return into function with what we want to return.
In this example we will not print sum and return from function and then print it.
Return generally put into the end of the function but there is no restriction about its location. We can use
return multiple times. Multiple usage with
if-else is popular.
Function must have code blocks or bodies to work correctly. But in some situations we have not complete the function bodies but want to create functions. We can call these functions empty functions. We can use
pass keyword to provide empty body which will do nothing.
As we can see there is operation or output from function named
While providing arguments or parameters the sequence is important. We need to put parameters in required sequence according to the function definition. But there is alternative usage where we do not need to comply with sequence. We can also skip some parameters and use their default values. We will provide parameters with their keyword.
We called function like below and all of them created same output.
While calling functions we need to provide all arguments or parameters to the function. If we do not provide we will get error. In some situations some parameters may be the same most of time and providing them in each function call is a trivial task. In order to make this type of usage more practical we can set some default values for arguments and while calling them if these parameters are not defined default values can be assumed by the function.
In this example we will assume name as
adam if it is not provided. Default arguments are specified after normal arguments in function definition.
Variable Length Arguments
Up to now we have defined functions with specific number of arguments. We strictly provided the parameters. But sometimes this is not a solution and prevent us to work with multiple variable length arguments. We can use variable length arguments to address this. We will put variable name argument at the end of parameters and put asterisk left side like
In this example we will create a function with variable argument.
for var in other:
We see that we loop all provided options arguments and print the to the terminal. The count of arguments is not important .