Function Arguments in Python

Functions are used to carry out a specific task in a program. Some user-defined functions are designed in such a way that they need an input which acts as a local variable to the function. This input is known as an Argument or Parameter.

If a function is defined with an argument, it becomes mandatory to provide the required value while calling it, otherwise, the code will generate errors.

Many of the Python built-in functions also need arguments. The print() function is the best example for this case. Whenever you call the print() function, an input is provided to it within these parentheses. Then the given value is displayed as output. So this is how the arguments work.

These are different ways to pass arguments to the functions. Let us find out.

  1. Default Arguments
  2. Arbitrary Arguments
  3. Arbitrary Keyworded Arguments

1. Default Arguments

When a function is created with an argument, you can pass a value to it at the same time with the help of an assignment operator(=). This becomes the default value of the parameter. In this case, you won't have to pass an input to that parameter because its default value is already set. These arguments are termed as Default Arguments.

Example:

#creating the function
def show_date(d=26, m=11, y=2018):
    print("Date: ",d,"/",m,"/",y)

#calling the function
show_date()

Output:

Date: 26 / 11 / 2018
  • This default value can also be changed by passing another value while calling the function. Let us see the previous example again with some modifications.
    #creating the function
    def show_date(d=26, m=11, y=2018):
        print("Date: ",d,"/",m,"/",y)
    
    #calling the function
    show_date(1,1)​

    Output:
    Date: 1 / 1 / 2018​

    In this example, we have to change the default value of the first two arguments.

  • If the list of arguments contains any argument with no default value, then you will have to provide the required value to those parameters in the function call.
    Example:
    #creating the function
    def person(name, age = 35, weight = 70):
        print('Name : ',name)
        print('Age: ',age)
        print('Weight: ',weight)
    
    #calling the function
    person('Rounak Agarwal')​​
  • Output:

    Name: Rounak Agarwal
    Age: 35
    Weight: 70

    In this example, three arguments are passed to the function person(), out of which the first argument does not have a default value while the other two arguments are default.

2. Arbitrary Arguments (*args)

You may face a situation where you are not aware of the number of arguments needed for the function. Then you should use the arbitrary arguments.

To pass an arbitrary argument, an asterisk(*) is used right before the argument while creating the function. Thus, the interpreter understands that this function is going to have more than one parameter.

The values of multiple arguments are stored in a Tuple.

Let us see an example:

#creating function with an arbitrary argument.
def person(*info):
    print('Information :', info)
    #creating a for loop to print the value of each argument.
    for x in info:
    print(x)
#calling the function.
person('Virat',25,'India',70)

Output:

Information : ('Virat', 25, 'India', 70)
Virat
25
India
70

3. Arbitrary Keyworded Arguments (**kwargs)

As the name suggests, keyworded arguments have a keyword = value pair.

Before we proceed, just have a look at the output of the previous program again. Can you figure out what does 25 or 70 indicate? No.
Thus, to make the output more clear and understandable, keyworded arguments are used.

To pass keyworded arguments, double asterisk (**) symbol is used which is followed by the argument in the definition of the function. When the function is called, the actual parameters are passed in the form of 'keyword = value' pair. You can pass any number of pairs.

These values along with their keywords are stored in a dictionary and retrieved in the same form.

Let us modify the previous example using keyworded arguments to understand the use of it.

#creating function with an arbitrary argument.
def person(**info):
    print('Information :', info)
    #creating a for loop to print value of each argument.
    for x,y in info.items():
    print(x,y)
#calling the function.
person(Name='Virat',Age=25,Country='India',Weight=70)

Output:

Information : {'Name': 'Virat', 'Age': 25, 'Country': 'India', 'Weight': 70}
Name Virat
Age 25
Country India
Weight 70