The function is a code block that is named and can be called again and again with this name. Functions are very useful in order to create code for repetitive tasks and use this code with a single function call.
The def keyword is used to define function and set its name. The function definition syntax is like below.
def FUNCTION_NAME(PARAMETERS): CODE return RETURN_VALUE
- FUNCTION_NAME is the name of the function which is used to call this function later from other parts of the code.
- PARAMETERS are single or multiple values that can be passed into the function in every call. This is optional.
- CODE is the code block of the function or body of the function which is executed every time the function is called.
- return RETURN_VALUE is used to return a value or object to the function caller. This parameter is optional.
Let's create a function that is very simple in order to understand the basics of the Python function definition. We will create the function named say_hello() without a parameter. As we can see the function name can be a single letter or multiple words where space can not be used. We have used the underscore for multiple words. After the function name the parameters are defined inside parenthesis but as we do not used in this create the parenthesis will be empty. The last character is double-colon which means the end of the function name definition and the next line will be the function body. the function body starts with spaces that are up to the developer but should be the same with other Python code. Generally, 3 spaces are enough and this is called indent. We can put single or multiple lines function body all of them should be indented.
def say_hello(): print("Hello World")
Now calling a function is much easier than defining a function. We will just provide the name of the function and if required the function parameters. In this case, the say_hello() function does not require any parameters.
def say_hello(): print("Hello World") say_hello()
Parameters make functions very powerful by adapting the functions for different values and cases. Provided parameters are used inside the function body and execute accordingly. The function parameter is optional. Single or more parameters can be defined for a function. Parameters are named according to the developer but using related names will make the code more readable and understandable.
In the following example we will define the function named multiply which accepts two parameters to multiply.
def say_hello(name,surname): print("Hello",name,surname) say_hello("ismail","baydan")
If the reuqired parameters are not provided properly when calling the function the following error occurs.
TypeError: say_hello() missing 2 required positional arguments: 'name' and 'surname'
Function Return Value
Normally a function may or may not return a value. It is optional but generally functions are used to return a value after the function body code execution. Below we will create a function which is named multiply to muliply provided parameters and return the result. The returned values can be used like assingin a variable or printing as output.
def multiply(a,b): return a*b result = multiply(2,3) print(result) print(multiply(3,3))
Function Keyword Parameters
By default, the function parameters are assigned in sequential order. For example, def multiply(a,b) definition can be called as multiply(2,3) and the parameter a will be assigned as 2, and parameter b will be assigned as 3. We can change the sequence of the parameters by using keyword parameters and providing parameter values as keywords.
def multiply(a,b): return a*b result = multiply(b=5,a=12)
Default Parameter Value
A function may contain parameters where the value is generally used as the same. Setting this parameter value as the same again and again is a trivial task. The default parameter value can be used in order to provide a default value that will be used if the given parameter is not provided. In the following example, we will set a value as 1 as a default parameter value. We will only provide the b parameter when calling the multiply() function.
def multiply(a=1,b): return a*b result = multiply(b=5)