The header for a function looks like this:
def function_name(parameter1, parameter2, ...):
The def keyword indicates that you're defining a function. The function name will be how you refer to this function when you call it. Parameters, if needed, give the values for variables which will be used only inside the function. When you call the function you must give an argument for each parameter.
For example, say you have defined the following two functions:
def greet():
print("Hello!")
def greet_by_name(name):
print("Hello, " + name + "!")
def greet_by_full_name(first_name, last_name):
print("Hello, " + first_name + " " + last_name + "!")
I could call them by using the name of the function, filling in parameters with arguments as needed.
greet()
greet_by_name("McClung")
greet_by_full_name("Albert", "Einstein")
Notice that each function had to be called by name, and each function had to be given the appropriate number of parameters.
To fill in the arguments for a function, you can put them in the correct position (positional), or you can assign an argument to a parameter by name (keyword). Let's look at an example of both:
def print_three(first, second, third):
print(first)
print(second)
print(third)
Suppose you want first to be "coding", second to be "in", and third to be "Python". You can call them positionally by simply putting the arguments in the appropriate order:
print_three("coding", "in", "Python")
But you can also call them in any order with keyword arguments. This can be useful if you (or the person who will be reading your code) doesn't know the parameters or what order they're in.
print_three(second = "in", third = "Python", first = "coding")
Occasionally, you might want to do a mix of these two, but if you do, all positional arguments must come first.
print_three("coding", third = "Python", second = "in")
print_three("coding", third = "Python", "in")