Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Types of Functions in Python

by suresh

In real-time, a Python function may define with or without parameters, and a function may or may not return a value. It entirely depends upon the user requirement. 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. Function with no argument and no return value.
  2. Function with no argument and with a Return value.
  3. Python Function with argument and No Return value.
  4. Function with argument and return value.

NOTE:

  • From the above, 1 and 3 types of functions in Python do not return any value when the function called. So, while defining the function, we can avoid the return keyword.
  • When we call the 2 and 4 types of functions in Python, 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 the function, We won’t pass any arguments to the function. This type of function won’t return any value when we call the function.

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

Python Function with No argument and No Return value Example

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

# Python Function with No Arguments, and No Return Value
def Adding():
    a = 20
    b = 30
    Sum = a + b
    print("After Calling the Function:", Sum)
Adding()
Types of Functions in Python programming 1

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

We declared the integer variables a, b, and assigned 20 to a and 30 to b.

In the next line, we calculate the sum using Arithmetic operator ( + )

    a = 20
    b = 30
    Sum = a + b

The below Python print statement is to print the output.

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

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

Python Function with no argument and with a Return value

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

Function with No arguments and with a Return value Example

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

# Python Function with No Arguments, and with Return Value
def Multiplication():
    a = 10
    b = 25
    Multi = a * b
    return Multi
print("After Calling the Multiplication Function: ", Multiplication())
Types of Functions in Python programming 2

Within the Multiplication () function, 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 function, and we are using the function name inside the print statement. (nothing but calling the function)

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

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

Python Function with argument and No Return value

If you observe the above 2 methods, No matter how many times you executive, it 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 type of function in Python allows us to pass the arguments to the function while calling the function. But, This type of function in Python won’t return any value when we call the function.

Python Function 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 function to Multiply them.

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

Multiplications(10, 20)   
Types of Functions in Python programming 3

Within the Multiplication(a, b) function, We declared the variables of Multi, and also, we also had (a, b) arguments in the function. It means this function 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)

As you can see from the above output, we called the Multiplication function with different values, and it is giving the output as per the values.

Python Function with argument and Return value

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

Python Function with arguments and Return value Example

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

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

Within the Addition(a, b) function, We declared the variables of Sum, and also, we also had (a, b) arguments in the function. It means this function 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)

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

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy