A function in Python is a collection of connected statements that carry out a certain activity.
Functions assist in segmenting our program into manageable, modular portions. Our program becomes more organized and controlled as it gets bigger and bigger.
If you wish to learn more about Python, please do watch this Python Training video on Youtube.
Furthermore, it avoids repetition and makes the code reusable.
def function_name(parameters):
"""docstring"""
statement(s)
Above shown is a function definition that consists of the following components.
It begins with the keyword def in the function header.A name for the function that serves as a unique identifier.The conventions for creating Python identifiers apply to naming functions as well.The variables (arguments) that we use to supply values to a function. They are not required.The function header ends with a colon (:).Documentation string (docstring) that is optionally provided to explain the function's purpose.The body of the function is composed of one or more legitimate Python statements. The indentation level for each statement must be the same (usually 4 spaces).A return statement that is optional allows the function to return a value.
Sign in to leave a comment.