What are the Types of Functions in Python

Video Tutorial
FREE
Types of functions thumbnail
This video belongs to
Python and SQL for Data Science
8 modules
Certificate
Topics Covered

What are the types of Functions in Python?

Functions are the basic building block of any Python program, defined as the organized block of reusable code, which can be called whenever required.

A function is used to carry out a specific task. The function might require multiple inputs. When the task is done executing, the function can or can not return one or more values.

There are two types of functions in python:

  • User-Defined Functions - these types of functions are defined by the user to perform any specific task
  • Built-in Functions - These are pre-defined functions in python.

image alt

Built-in Functions

Built-in functions are already defined in python. A user has to remember the name and parameters of a particular function. Since these functions are pre-defined, there is no need to define them again.

Some of the widely used built-in functions are given below:

Example:

Output:

In the above program, we created a list and stored a few numbers in it, then called various in-built functions on the list and displayed the output.

Some of the widely used python built-in functions are:

FunctionDescription
len()Returns the length of a python object
abs()Returns the absolute value of a number
max()Returns the largest item in a python iterable
min()Returns the largest item in a python iterable
sum()Sum() in Python returns the sum of all the items in an iterator
type()The type() in Python returns the type of a python object
help()Executes the python built-in interactive help console
input()Allows the user to give input
format()Formats a specified value
bool()Returns the boolean value of an object

User-Defined Functions

These functions are defined by a programmer to perform any specific task or to reduce the complexity of big problems and use that function according to their need.

Example:

Output:

In the above program, we have created our program that subtracts two numbers passed as arguments and displays the result. Such types of functions are known as user-defined functions. Use this Python Online Compiler to compile your code.

Before moving ahead, do check out our Python course for beginners and learn Python from scratch!

Advantages of functions in Python

  • Helps in increasing modularity of code – Functions in python help the user to divide the program into smaller parts and solve them individually, thus making it easier to implement.

  • Minimizes Redundancy – Python functions help us to save the effort of rewriting the whole code. All we got to do is call the function once it is defined.

  • Maximizes Code Reusability – Once a function is defined in python, it can be called as many times as needed, thus enhancing code reusability.

  • Improves Clarity of Code – Since a large program is divided into sections with the help of functions, it helps increase the readability of code while ensuring easy debugging.

Learn More

For more in-depth information regarding functions in python, go to this article.

Conclusion

  • Functions are blocks of code written to carry out a specified task.
  • There are two types of functions in python:
    • User-Defined Functions - these types of functions are defined by the user to perform any specific task
    • Built-in Functions - these types of functions are pre-defined in python
  • Functions in python help us increase modularity, minimize redundancy, maximize reusability and improve code quality.