A function
is a block of code that is executed when the function is called. A function consists of no or multiple statements. But in general multiple statements are put into a function. These statements are executed in a row after the function call. The function should be called explicitly in Python. There are different type of function which should be called according to their types, parameters, and requirements. During a function call some data, parameters can be passed into a function. A function may or may not return some data.
Create A Function
A function can be created by using the keyword def
in Python. Then the name of the function and parameters of the function can be provided according to the situation. Below you can see a generic syntax to create a function.
def FUNCTION_NAME(PARAMETERS):
FUNCTION_BODY
RETURN
- FUNCTION_NAME is the name of the function which will be used to call the function from other parts of the code.
- PARAMETERS is a single or more parameter provided to the function during calling it. PARAMETERS is optional.
- FUNCTION_BODY is the function body code that will be executed in every call of the function.
- RETURN is optional where a function can return some data, value, or variable data to the caller.
Call A Function without A Parameter
First, we will create a function that does not have any parameter which will simply execute its body code. In the following example, there will be no return value. The function will be named as say() and simply prints “Hello PythonTect” to the standard output. We will just call the say() to execute this function.
def say():
print("Hello PythonTect")
say()
We can also call functions from other modules bu first we will import this module and refer to this module name.
Call A Function with Parameter
Using parameters makes the functions very powerful where the same function can be used for different cases and situations. Parameters are optional but can be used easily by adding to the function definition after the function name inside brackets. We can call this function multiple times with different parameters where the result will be different too. In the following example we created a function named say() and added the parameter text which will be printed to the standard output with the print() method. The text parameter will be called when the say(text) function is called.
def say(text):
print(text)
say("Hello PythonTect")
say("Hello LinuxTect")
There is no limit about the count of the function parameters where one or more parameters can be specified without any problem. In the following example we will add multiple parameters like name, age, text.
def say(text, name , age ):
print(text+" "+name+". Your age is "+age)
say("Hello","PythonTect","1")
say("Hi","LinuxTect","1")
Call A Function with Default Parameter
Even parameters provides dynamic behaivour for the functions in some cases some parameters can use default values which means there is not need to specfiy the parameter value in every call of the function.
def say(text, name = "PythonTect" , age = "1" ):
print(text+" "+name+". Your age is "+age)
say("Hello","PythonTect","1")
say("Hello","PythonTect")
say("Hello")
The output will be like below.
Hello PythonTect. Your age is 1 Hello PythonTect. Your age is 1 Hello PythonTect. Your age is 1
Call A Function with Arbitrary Parameters/Arguments
Functions may have different numbers of parameters where in some cases the parameter count can not be set during the function definition. We can call these functions which accept arbitrary parameters by using * before the parameter name. This parameter stores multiple values as a list and can be returned by using list indexes.
def say( *param ):
print(param[0]+" "+param[1]+". Your age is "+param[3])
say("Hello","PythonTect","1")
say("Hi","LinuxTect","1","Test")
Alternatively the artbitrary parameters can be named with keywords and can be accessed inside the function body. The ** is used to specify the parameter as arbitrary keyword argument. Python generally uses the **kwargs to name arbitrary keyword arguments.
def say( **param ):
print(param["text"]+" "+param["name"]+". Your age is "+param["age"])
say(text="Hello",name="PythonTect",age="1")
Get The Return Value From Function
Functions are not used only to execute its body and complete the function call. The function can also return some values in different types like string, integer, list, etc. In the following example, the function returns a string value. The returned values can be set into a variable.
def say(text):
return "Hello " + text
p = say("PythonTect")
l = say("LinuxTect")