Python – Function Arguments

You may create a function in Python that takes a variable number of arguments. You will learn how to define such functions utilizing default, keyword, and arbitrary arguments in this post.

Arguments

We learned how to define a function and call it in the user-defined function subject. Otherwise, the call to the function will fail. Here’s an illustration.

def greet(name, msg):
    """This function greets to
    the person with the provided message"""
    print("Hello", name + ', ' + msg)

greet("Monica", "Good morning!")

Output

Hello Monica, Good morning!

Here, the function greet() has two parameters.

This function operates easily and without errors because we called it with two arguments.

The interpreter will display an error message if we call it with a different amount of arguments. A call to this method with one and no arguments, as well as their associated error messages, is shown below.

>>> greet("Monica")    # only one argument
TypeError: greet() missing 1 required positional argument: 'msg'
>>> greet()    # no arguments
TypeError: greet() missing 2 required positional arguments: 'name' and 'msg'

Variable Function Arguments

Functions had a set number of arguments up until now. Other techniques to define a function that can take a variable number of parameters exist in Python.

Three different forms of this type are described below.

Python Default Arguments

Function arguments can have default values in Python.

Using the assignment operator (=), we can give an argument a default value. Here’s an illustration.

def greet(name, msg="Good morning!"):
    """
    This function greets to
    the person with the
    provided message.

    If the message is not provided,
    it defaults to "Good
    morning!"
    """

    print("Hello", name + ', ' + msg)


greet("Kate")
greet("Bruce", "How do you do?")

Output

Hello Kate, Good morning!
Hello Bruce, How do you do?

Using the assignment operator (=), we can give an argument a default value. Here’s an illustration.

The parameter msg, on the other hand, has the value “Good morning!” as its default value. As a result, it is not required during a call. If a value is specified, it will take precedence over the default value.

A default value can be set for any number of arguments in a function. However, if we have a default argument, we must likewise have default values for all the arguments to its right.

Non-default arguments, in other words, cannot be followed by default arguments. For example, if the function header had been defined as:

def greet(msg = "Good morning!", name):

We would get an error as:

SyntaxError: non-default argument follows default argument

Python Keyword Arguments

The function calls are linked to keyword arguments. When you utilize keyword arguments in a function call, the caller uses the parameter name to identify the parameters.

Because the Python interpreter can use the keywords provided to match the values with parameters, you can skip arguments or place them out of order. The printme() method can also be called with keywords in the following ways:

# 2 keyword arguments
greet(name = "Bruce",msg = "How do you do?")

# 2 keyword arguments (out of order)
greet(msg = "How do you do?",name = "Bruce") 

1 positional, 1 keyword argument
greet("Bruce", msg = "How do you do?")           

As can be seen, during a function call, we can blend positional and keyword arguments. However, we must remember that keyword arguments must come after positional arguments.

There will be issues if a positional argument is used after keyword arguments. For instance, consider the following function call:

greet(name="Bruce","How do you do?")

Will result in an error:

SyntaxError: non-keyword arg after keyword arg

Python Arbitrary Arguments

We don’t always know how many arguments will be supplied into a function ahead of time. We can deal with this type of circumstance in Python by calling functions with an arbitrary number of arguments.

To indicate this type of argument, we use an asterisk (*) before the parameter name in the function specification. Here’s an illustration.

def greet(*names):
    """This function greets all
    the person in the names tuple."""

    # names is a tuple with arguments
    for name in names:
        print("Hello", name)


greet("Rocky", "Sunil", "Anil", "Bishal")

Output

Hello Rocky
Hello Sunil
Hello Anil
Hello Bishal

We’ve called the function with numerous arguments in this case. Before being provided into the function, these arguments are wrapped into a tuple. We utilize a for loop inside the method to get all of the arguments back.

Leave a Comment

Your email address will not be published.