In Python, we can define functions to make our code reusable, more readable, and organized. This is the basic syntax of a Python function:
1 2 3 4 |
def <function_name>(<param1>, <param2>, ...): <code> |
Tip: a function can have zero, one, or multiple parameters.
A function with no parameters has an empty pair of parentheses after its name in the function definition. For example:
1 2 3 4 5 6 |
def print_pattern(): size = 4 for i in range(size): print("*" * size) |
This is the output when we call the function:
1 2 3 4 5 6 7 |
>>print_pattern() **** **** **** **** |
You have to write an empty pair of parentheses after the name of the function to call it.