Python Function Arguments
Arguments
Data can be passed into functions through arguments.
Arguments are defined after the function name inside the parentheses. You can add as many arguments as you want, just use a comma to separate them.
Arguments are often abbreviate to args in Python documentations.
In the following example, we will declare a function with one argument ("fname"). Then, when the function is called, we pass along the person's name, which is used inside the function to greet.
def greet(fname):
"""
This function greets
to the person name passed in
as a parameter
"""
print("Hello, " + fname)
greet("Robert")
greet("Richard")
Output
Hello, Robert
Hello, Richard
Arguments or Parameters
The terms argument or parameter can be meaning the same thing: data that are passed into a function.
However, from a function's perspective, an argument is the value sent to the function when it is called, and a parameter is the variable listed inside the parentheses in the function definition.
Number of Arguments
By default, to call a function, you must use the right number of arguments. It means that if you have a function that is expecting two arguments, you have to call the function with two arguments, not more or less.
In the following example, we will define a function that expects two arguments and gets two arguments.
def greet(fname, lname):
print("Hello, " + fname + " " + lname)
greet("James", "Martinez")
greet("Donna", "Williams")
Output
Hello, James Martinez
Hello, Donna Williams
If you try to call the greet()
function with other than two arguments, an error will occur.
def greet(fname, lname):
print("Hello, " + fname + " " + lname)
greet("Donna")
Output
TypeError
2 print("Hello, " + fname + " " + lname)
3
----> 4 greet("Donna")
TypeError: greet() missing 1 required positional argument: 'lname'
Variable Function Arguments
Python offers the possibility to define a function that can have variable (changeable) number of arguments.
There are different forms of variable function arguments that are described below.
Python Default Arguments
In Python, function arguments can have default values.
A default value can be provided to an argument using the assignment operator (=
).
In the following example, we will assign a default value to the msg
parameter.
def greet(fname, msg="Welcome"):
"""
This function greets
to the person with the provided message.
"""
print("Hello, " + fname + ", " + msg)
greet("James")
greet("Stephanie", "How are you?")
Output
Hello, James, Welcome
Hello, Stephanie, How are you?
As we can see above, the parameter fname
does not have a default value and is always required during a function call.
On the other side, the parameter msg
is assigned with a default value of "Welcome". So it is optional during a function call. If a value is passed, it will overwrite the default value.
Once you have a default argument, all the arguments to its right must also have default values.
If you try to use a non-default argument after a default argument, it will raise an error.
def greet(msg="Welcome", fname):
"""
This function greets
to the person with the provided message.
"""
print("Hello, " + fname + ", " + msg)
greet("James")
Output
SyntaxError: non-default argument follows default argument
Note: In a function, non-default arguments cannot follow default arguments.
Python Keyword Arguments
When a function is called with some values, these values get assigned to the arguments according to their position.
For example, in the above, the greet()
function was called as greet("Stephanie", "How are you?")
, the value "Stephanie"
gets assigned to the argument fname
, and the value "How are you?" gets assigned to the argument msg
.
In Python, you can call a function using keyword arguments. When you call functions using keyword arguments, the position of the arguments can be changed.
In the following example, all calls to the greet()
function are valid and produce the same result.
def greet(fname, msg):
"""
This function greets
to the person with the provided message.
"""
print("Hello, " + fname + ", " + msg)
# 2 keyword arguments
greet(fname="James", msg="How are you?")
# 2 keword arguments (out of order)
greet(msg="How are you?", fname="Stephanie")
# 1 positional, 1 keyword argument
greet("Paul", msg="How are you?")
Output
Hello, James, How are you?
Hello, Stephanie, How are you?
Hello, Paul, How are you?
Note: We can mix positional arguments with keyword arguments when we call a function. However, we must always write keyword arguments after positional arguments.
In the following example, we will have a positional argument after a keyword argument, which will raise an error.
def greet(fname, msg="Welcome"):
"""
This function greets
to the person with the provided message.
"""
print("Hello, " + fname + ", " + msg)
greet(fname="Stephanie", "How are you?")
Output
SyntaxError: positional argument follows keyword argument
Python Arbitrary Arguments
In some scenarios, we do not know in advance the number of arguments that will be passed into a function. Python allows us to handle this kind of situation through function calls using an arbitrary number of arguments.
To use the arbitrary arguments, you can use an asterisk *
before the parameter name to make this kind of argument.
In the following example, we will use an arbitrary argument to call the greet
function.
def greet(*fnames):
"""
This function greets all the persons passed as argument.
"""
for fname in fnames:
print("Hello, " + fname)
greet("James")
greet("Harry", "Stephanie", "William")
Output
Hello, James
Hello, Harry
Hello, Stephanie
Hello, William
In the above, we have called the function with a different number of arguments. The arguments get wrapped up into a tuple before being passed into the corresponding function. Inside the function, we use a for loop to iterate through all the passed arguments.