Types of Functions in Python

In real-time, a Python function may define with or without parameters, and it may or may not return a value. It entirely depends upon the user’s requirements. In this article, we explain to you the types of functions in Python Programming language with examples.

In Python programming, as per our requirement, We can define the User defined functions in multiple ways. The following are the list of available types of functions in Python.

  1. With no argument and no return value.
  2. With no argument and with a Return value.
  3. Argument and No Return Value.
  4. With argument and return value.

From the above, 1 and 3 types of functions in Python do not return any value when they are called. So, while defining them, we can avoid the return keyword. When we call the 2 and 4 types of functions, they return some value. So, we have to use the return keyword.

Types of Functions in Python

The following examples explain the list of available types of functions in Python programming.

Python Function with No argument and No Return value

In this type of function in Python, While defining, declaring, or calling them, We won’t pass any arguments to them. This type of function won’t return any value when we call them.

Whenever we are not expecting any return value, we might need some print statements as output. In such a scenario, we can use these types of functions in Python.

No argument and No Return value Example

In this Python type of function program, we are going to calculate the Sum of 2 integer values and print the output from the user-defined itself.

# With No Arguments, and No Return Value
def Adding():
    a = 20
    b = 30
    Sum = a + b
    print("After Calling :", Sum)
Adding()

No arguments and no Return value output

After Calling : 50
>>> Adding()
After Calling : 50

If you observe the Addition(), We haven’t passed any arguments /parameters to the Addition()

We declared the integer variables a, and b, and assigned 20 to a and 30 to b. In the next line, we calculate the sum using the Arithmetic operator ( + )

    a = 20
    b = 30
    Sum = a + b

The below Python print statement is to print the output. Whenever we call the Adding(), it prints the same output because a and b have fixed values inside the method.

print("After Calling :", Sum)

Python Function with no argument and with a Return value

In this type of function in Python, We won’t pass any arguments to it while defining, declaring, or calling it. When we call this type of function, it returns some value.

No arguments and with a Return value Example

In this Python type of function in the program, we are going to calculate the multiplication of 2 integer values using the user-defined without arguments and return keyword.

# With No Arguments, and with Return Value
def Multiplication():
    a = 10
    b = 25
    Multi = a * b
    return Multi
print("After Calling the Multiplication : ", Multiplication())

No arguments and with a Return value output

After Calling the Multiplication : 250
>>> Multiplication()
250

Within the Multiplication (), We haven’t passed any arguments /parameters. Next, We declared the integer variables of Multi, a, b, and we assigned 10 to a, and 25 to b. In the next line, we Multiplied both a and b using an Arithmetic operator ( * ).

a = 10
b = 25
Multi = a * b

Lastly, the print statement is to print the output. Remember, we are using the print statement outside the defined method and the name inside the print statement. (nothing but calling the method)

print("After Calling the Multiplication : ", Multiplication())

Here also, Whenever we call the Multiplication(), it prints the same output because a and b have fixed values inside it.

Python Function with argument and No Return value

If you observe the above 2 types of functions, No matter how many times you executive, Python gives the same output. We don’t have any control over the variable values (a, b) because they are fixed values. In real-time, we mostly deal with dynamic data means we have to allow the user to enter his own values rather than fixed ones.

This Python type of function allows us to pass the arguments while calling it. But, This type won’t return any value when we call it.

With argument and No Return value Example

This program for the type of function in Python allows the user to enter 2 integer values, and then, We are going to pass those values to the user-defined method to Multiply them.

# With Arguments, and NO Return Value
def Multiplications(a, b):
    Multi = a * b
    print("After Calling the Function:", Multi)

Multiplications(10, 20)   

We called the Multiplication method with different values, and it gives the output as per the values.

Types of Functions in Python programming 3

Within the Multiplication(a, b), We declared the variables of Multi, and also, we also had (a, b) arguments in it. It means this one allows the user to pass 2 values.

In the next line, we added both a and b using an Arithmetic operator ( * )

Multi = a * b

In the next line, the print statement is to print the output.

print("After Calling the Function:", Multi)

Python Function with argument and Return value

This type of Python function allows us to pass the arguments to it while calling it. This type of function returns some value when we call them. This type of user-defined method called a fully dynamic, means it provides maximum control to the end-user.

With arguments and Return value Example

This type of function in the Python program allows the user to enter 2 integer values. Then, we pass those values to the user-defined method to add those values and return the value using the return keyword.

# Wwith Arguments, and Return Value
def Addition(a, b):
    Sum = a + b
    return Sum
# We are calling it Outside the Definition
print("After Calling :", Addition(25, 45))
Types of Functions in Python programming 4

Within the Addition(a, b), We declared the variables of Sum, and also, we also had (a, b) arguments in it. It means this allows the user to pass two values. In the following line, we added both a and b using an Arithmetic operator ( * )

Multi = a * b

In the next line, the print statement is to print the output of the multi-variable.

print("After Calling :", Multi)

As you can see from the above output, we called the addition method with different values, and it gives the output as per the values.

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.

Comments are closed.