Articles

Function Arguments in Python

5 min read

Python allows the ability to define and call functions with arguments.

What is an argument in Python?

Arguments allow us to pass values into a function, which can then use these values to perform specific tasks. In this article, we will learn about Python arguments, exploring the different types of arguments, how to define them, and how to call functions with arguments.

Types of Function Arguments in Python

There are four main types of function arguments in Python:

  • Python Positional Arguments: These are the most basic types of arguments and are passed to a function by matching the order in which they are defined. The values are assigned to the parameters in the same order as they appear in the function definition.
  • Python Keyword Arguments: In this type of argument, we pass the values to the parameters using the parameter name, making it more explicit and less prone to error.
  • Default Arguments in Python: These are arguments that have a default value defined in the function definition. If a value is not passed for these arguments during a function call, the default value is used instead.
  • Python Variable-length Arguments: In some cases, we may not know in advance how many arguments will be passed to a function. For this, Python provides two types of variable-length arguments: *args and **kwargs. The former allows you to pass a variable number of positional arguments to a function, while the latter allows you to pass a variable number of keyword arguments.

Note: We can mix and match different types of arguments in a single function, depending on our needs.

Python Positional Function Arguments

Positional arguments are the most basic type of function arguments in Python. They are passed to a function by matching the order in which they are defined. The values are assigned to the parameters in the same order as they appear in the function definition.

Example of Python positional argument:

def greet(name, message):
    print("Hello, {}. {}".format(name, message))

greet("John", "How are you today?")
# output: Hello, John. How are you today?

Here, the greet function takes two positional arguments: name and message. When we call the greet function, we pass the values “John” and “How are you today?” as arguments. The values are then assigned to the name and message parameters in the same order they are defined.

Note: The positional arguments must always be passed in the correct order, as defined in the function definition. If we pass the arguments in the wrong order, the function may produce unexpected results.

Keyword Function Arguments in Python

Keyword arguments in Python allow us to pass values to a function using the parameter name, rather than the order in which they are defined. This makes the function call more explicit and less prone to error.

Example:

def greet(name, message):
    print("Hello, {}. {}".format(name, message))

greet(message="How are you today?", name="John")
#output: Hello, John. How are you today?

Here, we call the greet function using keyword arguments, rather than positional arguments. We specify the parameter names name and message, and pass the values “John” and “How are you today?” as arguments.

Note: When using keyword arguments, the order of the arguments doesn’t matter. We can pass the arguments in any order we may like, as long as we specify the parameter names correctly.

Default Function Arguments in Python

Default arguments in Python allow us to specify a default value for a function parameter in case a value is not passed during the function call.

Example of default function argument in Python:

def greet(name, message="Good morning"):
    print("Hello, {}. {}".format(name, message))

greet("John")
#output: Hello, John. Good morning

Here, the greet function takes two arguments: name and message, with the latter having a default value of “Good morning“. When we call the function with only one argument, “John“, the default value of “Good morning” is used for the message parameter.

Note: Default arguments must always come after positional arguments in the function definition. This is because positional arguments are required and must be passed, while default arguments are optional and only used if a value is not passed during the function call.

Variable-length Function Arguments in Python

Variable-length arguments in Python allow you to pass a variable number of arguments to a function. There are two types of variable-length arguments: *args and **kwargs. The former allows us to pass a variable number of positional arguments, while the latter allows us to pass a variable number of keyword arguments.

Here’s an example using *args:

def greet(*names):
    for name in names:
        print("Hello, {}".format(name))

greet("John", "Jane", "Jim")
#output: Hello, John
#        Hello, Jane
#        Hello, Jim

Here, the greet function takes a variable number of positional arguments, which are collected into a tuple names. When we call the function with three arguments, “John“, “Jane“, and “Jim“, the values are passed as a tuple to the names parameter. The function will then loop through the tuple and print a greeting for each name.

Here’s an example using **kwargs:

def greet(**kwargs):
    for key, value in kwargs.items():
        print("{}'s message: {}".format(key, value))

greet(John="Good morning", Jane="How are you today?", Jim="Hello")
#output: John's message: Good morning
#        Jane's message: How are you today?
#        Jim's message: Hello

Here, the greet function takes a variable number of keyword arguments, which are collected into a dictionary kwargs. When we call the function with three keyword arguments, John=”Good morning”, Jane=”How are you today?”, and Jim=”Hello”, the values are passed as a dictionary to the kwargs parameter. The function will then loop through the dictionary and print a message for each key.

Note: We can use *args and **kwargs together in the same function definition, allowing us to accept a combination of positional and keyword arguments of variable length.

Conclusion

Python functions provide a powerful tool for encapsulating and reusing code.

Understanding the different types of function arguments available in Python – positional, keyword, default, and variable-length – can help us write more efficient and flexible code.

Whether we are passing a fixed number of arguments or a variable number of arguments, we can use function arguments to write clean and easily maintainable code.

With the ability to pass arguments in a variety of ways, we can tailor our functions to meet the specific needs of our application and solve problems in a more efficient manner.

Do you want to be a Python expert, get online python tutor now!