Concept: Functions
A function is a reusable block of code that performs a single and specific task. Functions allow us to break our code into smaller, manageable pieces, making it easier to read, understand, and maintain.
A function is defined using the def keyword, followed by the function name and parentheses. Within the parentheses, we can define input parameters that the function needs. The code block within every function starts with a colon (:) and is indented.
A function can optionally return an output value using the return keyword and optionally contain a docstring that describes what the function does. This docstring is a string that is placed at the beginning of the function body and is enclosed in triple quotes (""").
def function_name(parameter1, parameter2):
"""
A function with 2 parameters that returns an output value.
"""
# code block
return output_value
def function_name2(parameter1):
"""
A function with 1 parameters that returns nothing.
"""
# Code block
# No return value (None is the default return value)
def function_name3():
# Code block
return output_valueThe functions can then be called by using their name followed by parentheses, passing in the required arguments.
result = function_name(arg1, arg2)
function_name2(arg1)
result3 = function_name3()