Types of Python Functions With Code Examples
Three Types of Functions in Python Explained
There are 3 types of functions in Python:
1. Help()
, which requests assistance, min(), which returns the minimal value, print()
, which outputs an object to the terminal, and so on are all built-in methods. More information on these functions may be found in this overview.
2. User-Defined Functions (UDFs), which are functions created by users to assist them;
3. Anonymous functions, often known as lambda functions, are functions that are not declared using the conventional def keyword.
Understanding Docstring
Docstrings define your function's behavior, such as the computations it conducts and the results it returns. These descriptions act as documentation for your function, allowing anyone who reads the docstring of your function to understand what it does without having to read the entire function definition.
Function docstrings are inserted in the line immediately following the function header, between triple quotation marks. 'Prints "Hello World"' is an adequate Docstring for your hello() method.
Example:
def my_function1():
'''Demonstrates '''
return None
print(my_function1.__doc__)
Output:
The return Statement
If you want to keep working with the result of your function and test out some operations on it, you'll need to use the return statement to return a value, such as a String or an integer. Consider the case below:
def python():
return("hello")
python()
hello