def my_function():
print("Hello from a function")
This is the basic structure of a function in Python
def
is the Python keyword or syntax for defining a function
my_function
is the name of the function, this can be anything of your choosing, but it should be a descriptive and logical name. We also use snake_case for naming functions in Python, which is a convention and not a rule.
()
the parenthesis are required for defining and calling functions, these are used to define the arguments, parameters, or inputs into the function.
:
the colon is important syntax to start a function definition block. All of the code beneath the colon must be indented as shown in the examples on this page.
print("Hello from a function")
is a function that is already defined in Python. Python comes with a lot of variables out of the box. The print function takes an argument or input and then prints it to the command line screen.
Function parameters allow our function to accept data as an input value. We list the parameters a function takes as input between the parentheses of a function ( )
.
Here is a function that defines a single parameter:
def my_function(single_parameter)
# some code
def trip_welcome(destination):
print("Welcome to Tripcademy!")
print("Looks like you're going to " + destination + " today.")
The parameter is the name defined in the parenthesis of the function and can be used in the function body.
The argument is the data that is passed in when we call the function, which is then assigned to the parameter name.
Multiple Parameters
def my_function(parameter1, parameter2, parameter3):
# Some code