Arguments and Parameters
We've now seen a number of examples of functions, and we've created a number of our own. However, we're yet to create a function like print
or len
which accepts some value inside the paratheses when we call it.
Remember in the last section we foolishly redefined print
and we got an error when trying to pass in some kind of value to our new print
function?
def print():
print("Hello, world!")
print()
print("Bye, world!") # Error
What exactly was the error that we received? It was in fact a TypeError
, and the error message was as follows:
TypeError: print() takes 0 positional arguments but 1 was given
Let's explore what this error means in more detail, and start creating functions that can accept values.
What is an argument?
Simply put, an argument is some value that we pass into a function when we call it. There are two types of argument: positional arguments, and keyword arguments. The error message above refers to a positional argument, and we'll cover this type of argument first.
For now, it's enough that we understand the error message above. We provided an argument to the new print
function - the string "Bye, world"
- but our print function wasn't expecting any arguments. The print
function therefore had no idea what to do with the string we passed into it, and raised an error.
So, how do we tell a function to expect arguments?
What is a parameter?
The answer to our previous problem is that we use parameters.
You can think of parameters as variables that we define as part of defining a function. Their purpose is to store the values provided by our function arguments.
Parameters are placed inside the parentheses when we define a new function, with each parameter separated by a comma.
Let's create our first function using parameters, which we'll call add
. add
will take two arguments when we call it, and we will use the names x
and y
as parameters for the function. add
will then take the values of x
and y
and print the sum of these numbers to the console.
def add(x, y):
result = x + y
print(result)
add(2, 3) # 5
In the example above, when we call the function, x
is given a value of 2
, and y
is given a value of 3
.
Note that we can use these values anywhere inside the function body, like when we define the result
variable.
Remember that when a function finishes running, we no longer have access to the variables defined inside. This is also true of parameters. If we try to use x
or y
outside of our function, Python will raise a NameError
, claiming that the name x
or y
is not defined.
Positional arguments
Earlier in this section I said there are two types of arguments: positional arguments and keyword arguments.
Let's take a look at our add
function again, particularly the arguments we passed in when we called it.
def add(x, y):
sum = x + y
print(sum)
add(2, 3) # 5
Note that we didn't have to tell add
which paramater each argument belongs to. 2
was simply assigned to x
and 3
was assigned to y
automatically. These are examples of positional arguments.
By default, Python assigns arguments to parameters in the order they are defined. x
is our first parameter, so it takes the first argument: in this case 2
. y
is the second parameter, so y
was assigned the value 3
: the second argument we passed into add
.
In summary, positional arguments are arguments which are assigned to parameters based on their position. In other words, the order in which we pass them to a function.
Keyword arguments
Keyword arguments are a little different to positional arguments in that we don't have to pass them into the function in any order. Instead, we specify the name of the corresponding parameter directly.
For example, we can call our add
function using keyword arguments like so:
def add(x, y):
result = x + y
print(result)
add(x=2, y=3) # 5
Why would we use keyword arguments?
One of the main reasons to use keyword arguments is that they make it very clear what each value means. You're also not bound by the order the parameters were defined in. You can specify keyword arguments in any order you like.
Some functions also define mandatory keyword arguments, which are not accessible using a positional argument. For example, print
takes a keyword only argument called sep
which defines how print
should separate values when you provide several arguments. By default, this value is an empty space, but we can define it to be whatever string we want.
print(1, 2, 3, 4, 5, sep=' - ')
Would print:
1 - 2 - 3 - 4 - 5
Pretty nifty.
Mixing keyword and positional arguments
Notice that in the print example above, we actually combine the two types of argument. This is perfectly legal in Python, but there are some things we need to be careful about.
Returning once again to our add
function, we can mix positional and keyword arguments like so:
def add(x, y):
sum = x + y
print(sum)
add(2, y=3) # 5
But what happens when we try to call add
with the keyword argument first?
add(x=2, 3)
We get an error!
SyntaxError: positional argument follows keyword argument
Be careful when using positional and keyword arguments together! Keyword arguments must always be specified after positional arguments.
Limits to numbers of parameters
In Python 3.6 and lower, there is technically a limit to the number of parameters a function can accept, but the number is so large that it's not worth worrying about.