Python Functions - Syntax and How To Call Python Function
What are Functions in Python?
In Python, functions are actually the blocks of code that work only when you call them. The code in the function block includes relevant statements to perform certain logics or tasks.
The role of functions in Python is to save some common code or programs as a function and use them wherever required, instead of writing that code or program again and again. Functions can be called out, and the logic statements will work there.
Syntax of a Function in Python
The following components make up a function definition, as seen above.
def function_name(parameters):
"""docstring"""
statement(s)
The term def marks the beginning of the function header. The function's name can be used to identify it from others. In Python, the same rules apply to naming functions as they do to naming identifiers.
We pass values to a function using arguments. The function header is terminated by a colon (:).
Documentation string (docstring) can be used to describe what the function does. The function body is made up of one or more valid Python statements. The indentation level of all statements must be the same (usually four spaces). You can use a return statement to return value from function.
How to Call a Function in Python?
Use the function name followed by parentheses to call it.
Example:
def myfunction():
print(" function")
myfunction()
Output: