Defining functions
Defining a function
Steps to define a function:
-
Start with
def
keyword which introduces function definition -
def
keyword must be followed by a function name and the parenthesis containing parameters if the function would be expecting inputs. -
The body of the function starts on the next line and must be indented
-
The first statement of the function body can optionally be a function’s documentation string
docstring
def function_name(): function body
Example
Let’s define a function that prints a message
def my_function(): print("Hello from my function")
After we have defined the function above, now it’s time to call it
# calling a function my_function() # output: Hello from my function