Introduction to Functions
Learn about the usage of functions in Python.
What are functions?
We divide a problem into subproblems and resolve a specific subproblem or task using a function. A Python function is a block of code that performs a specific and well-defined task.
Advantages of functions
Two main advantages of functions are:
- They help us divide our program into multiple tasks. For each task, we can define a function. This makes the code modular.
- Functions provide a reuse mechanism. The same function can be called any number of times.
Types of function
There are two types of Python functions:
- Built-in functions like
len()
,sorted()
,min()
,max()
, etc. - User-defined functions
An example of a user-defined function is shown below. A function can be called any number of times.
Note: The body of the function must be indented appropriately.
Get hands-on with 1200+ tech skills courses.