A function is a section of the program or a block of code that is written once and can be executed whenever required in the program. A function is a block of self-contained statements which has a valid name, parameters list, and body. Functions make programming more functional and modular to perform modular tasks. Python provides several built-in functions to complete tasks and also allows a user to create new functions as well.
There are three types of functions:
- Built-In Functions: copy(), len(), count() are the some built-in functions.
- User-defined Functions: Functions which are defined by a user known as user-defined functions.
- Anonymous functions: These functions are also known as lambda functions because they are not declared with the standard def keyword.
Example: A general syntax of user defined function is given below.
- def function_name(parameters list):
- #— statements—
- return a_value