Define/Create Function In Python

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. As popular and complete programming and scripting language Python provides functions. There are different types of functions where some of them accept parameters, some of them do not require parameters, some of them return some data some of them do not return any data, etc. In this tutorial, we examine how to create or define different types of functions in Python.

Define Function

The def keyword is used to define a 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")

Call Function

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()

Function Parameters/Arguments

Parameters make functions very powerful by adapting the functions for different values and cases. Parameters also called arguments in an interchangeable way. 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 multiply provided parameters and return the result. The returned values can be used like assign 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)

Anonymous Functions

Anonymous functions are a bit different from all other function types and implementations. As its name suggests anonymous functions do name with the assignment name. This means anonymous functions do not defined by using the keyword def . Instead the keyword lambda is used to defined an anonymous function. The anonymous or lambda function has some requirements and restrictions.

  • Lambda functions may take single or more parameters for the function body.
  • Lambda functions can only return a single value as a result of the function execution.
  • Lambda functions have their own namespace where only provided parameters can be used. No other variables can be accessed from the global namespace.
  • Lambda function should be defined and implemented in a single line.

In the following example, we create an anonymous or lambda function where two parameters are accepted and are summed together and the result is returned. We can see that there is not need to use the statement return to return some value.

sum = lambda a, b: a+b;

result1 = sum(1,2)

result2 = sum(3,5)

Leave a Comment