Python

Function Arguments in Python – Default, Keyword and Arbitrary

We will talk about variable function parameters in this lesson. We learnt how to call functions with a specific amount of parameters in earlier lessons on Python functions and user-written functions. For instance, if we developed a function that may receive two arguments, we must supply those two arguments when calling the function. Here, we’ll look at how to use Default, Keyword, and Arbitrary parameters when calling the function with variable arguments.

Fixed positional function arguments

Let’s first look at the issues we receive when calling the function with variable parameters without utilizing the Default, Keyword, or Arbitrary arguments before we explore variable function arguments.

Here, we have defined the function demo(), which may take two arguments when called because it has two parameters: name and age.

def demo(name,age):
   """This function displays the
   age of a person"""
   print(name + " is " + age + " years old")

# calling the function
demo("Mohan","20")

Output:

Function Arguments in Python – Default, Keyword and Arbitrary

Let’s call this function with a varying amount of parameters to observe what happens:

As you can see in the output, we attempted to execute the function demo() with a single parameter and encountered an error.

def demo(name,age):
   print(name + " is " + age + " years old")

# calling the function
demo("Mohan")

Output:

TypeError: demo() missing 1 required positional argument: 'age'

We are calling the function in this case without any parameters, and we are encountering a problem.

def demo(name,age):
   print(name + " is " + age + " years old")

# calling the function
demo()

Output:

TypeError: demo() missing 2 required positional arguments: 'name' and 'age'

Variable Function Arguments in Python

The fixed function parameters in Python have been covered thus far. Additionally, we now know that executing a function with variable parameters will result in an error.

It’s time to learn how to use Default, Keyword, and Arbitrary parameters to pass variable arguments in functions.

Python Default Arguments

We can give the function argument default values. When we provide a function parameter a default value, the argument is no longer required to be sent during the function call and is instead considered optional. Let’s use an illustration to help you grasp this idea.

In this example, we’ve used the assignment (=) operator to give the parameter age its default value.

The following example’s argument name is required when calling the function because it lacks a default value. The argument age, on the other hand, has a default value and is not required when calling a function.

def demo(name, age = "30"):
   """
   This function displays the
   name and age of a person

   If age is not provided,
   it's default value 30 would
   be displayed.
   """

   print(name + " is " + age + " years old")

demo("Steve")
demo("Lucy", "20")
demo("Bucky", "40")

Output:

Function Arguments in Python – Default, Keyword and Arbitrary

Python Keyword Arguments

We now know that the values we supply during a function call are assigned to the appropriate parameters based on their location. If a function, for instance, is defined as follows: def demo(name, age): Moreover, we are invoking the function as follows: demo(“Steve”, “35”) when the argument’s name is given the value “Steve” and its age is given the value “35,” the argument is said to be. Positional arguments are what they are referred to as.

Using keyword arguments, Python enables non-positional argument passing. To further grasp this, let’s look at an example:

def demo(name, age):
   print(name + " is " + age + " years old")

# 2 keyword arguments (In order)
demo(name = "Steve", age = "35")

# 2 keyword arguments (Not in order)
demo(age = "20", name = "Mohan")

# 1 positional and 1 keyword argument
demo("Bucky", age = "40")

Output:

Function Arguments in Python – Default, Keyword and Arbitrary

Note: The third function call in the aforementioned example, demo(“Bucky”, age = “40”), takes both keyword and non-keyword arguments. The non-keyword argument in the example follows (comes after) the keyword argument. The following error would be generated if you attempted to insert the keyword argument before the non-keyword parameter:

SyntaxError: non-keyword arg after keyword arg

So please remember that the keyword argument must always come after the non-keyword argument when combining positional and keyword arguments (positional argument).

Python Arbitrary Arguments

Python gives us the option to use any number of parameters. This is especially helpful when we are unsure how many parameters the function will need in advance.

Using the asterisk (*) symbol, we declare the arbitrary arguments while defining a function.

def fruits(*fnames):
   """This function displays
   the fruit names"""

   # fnames is a tuple with arguments
   for fruit in fnames:
       print(fruit)

fruits("Orange","Banana","Apple","Grapes")

Output:

Function Arguments in Python – Default, Keyword and Arbitrary

In the example above, we are using the * symbol before the parameter fnames to enable us to take any number of arguments when calling a function.

Before being supplied to the function, all of the passed parameters are saved in a tuple. We use a for loop inside the method to display all of the provided arguments.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button