Python Function Arguments


Function Arguments

We can also pass values to the function in the parenthesis. These values are known as the arguments.

For example, name, id, salary, list of employees, etc. These arguments can be of any data type like integer, string, list, dictionary, etc.

In [1]:
def customer_details(name,id,city):
    print('Customer Name =',name,
          "\nCustomer id =",id,
          "\nCity =",city)
In [2]:
customer_details('Steve',120897,'New York')
Customer Name = Steve 
Customer id = 120897 
City = New York

Types of arguments

In Python, we have the following types of arguments:

  1. Positional arguments
  2. Keyword arguments
  3. Default arguments
  4. Arbitrary arguments(variable length arguments *args and *kwargs)

Positional Arguments

Positional arguments are the arguments that need to be passed in the correct predefined order.

For example, if a function expects name, age and gender as argument, then we need to pass name, age and gender in the same order. If we change the order, then the result may also change.

In [1]:
def employee_details(name,age,salary):
    print('Name =',name,
          "\nAge =",age,
          "\nSalary =",salary)
In [2]:
employee_details('Steve', 32, 20000)
Name = Steve 
Age = 32 
Salary = 20000

Note:If we change the position of the arguments, then the result will change.

In [3]:
employee_details('Steve', 20000, 32)
Name = Steve 
Age = 20000 
Salary = 32

Keyword Arguments

Keyword arguments are the arguments in which parameter name with equal to sign is used. For keyword arguments, we need not maintain the order.

In [1]:
def employee_details(name,age,salary):
    print('Name =',name,
          "\nAge =",age,
          "\nSalary =",salary)

Note: Here position of the arguments can change.

In [2]:
employee_details(name = 'Steve', salary = 20000, age = 32)
Name = Steve 
Age = 32 
Salary = 20000

Default Arguments

We can set some values of the arguments while defining the function itself. These values are known as default values. This value is used when the value is not passed from the calling statement.

Function with default status.

In [1]:
def cust_details(id, status='Active'):
    print('Customer id',id,"is",status)

When we pass status it uses value we passed.

In [2]:
cust_details(25,'Inactive')
Customer id 25 is Inactive

When we don’t pass status it uses default value.

In [3]:
cust_details(35)
Customer id 35 is Active

Variable length arguments

Sometimes, the number of arguments to be passed to the function are not fixed. For these cases, we have variable length arguments.

These are of two types:

  1. Variable length positional arguments(*args)
  2. Variable length keyword arguments(*kwargs)

1. *args(Variable length positional arguments)

While working on the project we may come across a situation that we don’t know the number of arguments we want to pass to the function. So to solve this problem we have a provision in python to use an asterisk(*) symbol before the parameter name in the function, for example, *args. Most of the time you will see people use the “args” keyword but “args” is not the reserved keyword in python, we can use any name instead of “args” but use the asterisk(*) as a prefix.

Function with customer details as args.

In [1]:
def cust_details(*args):
    print(args)
In [2]:
cust_details("Steve",2680,'USA')
('Steve', 2680, 'USA')

Fetching values from *args.

In [3]:
def cust_details_value(*args):
    print("Customer Name =",args[0])
In [4]:
cust_details_value("Steve",2680,'USA')
Customer Name = Steve

Function using a random name other than args.

In [5]:
def emp_details(*employee):
    print("Employee Name =",employee[0])
In [6]:
cust_details("Gary",2780,'UK')
('Gary', 2780, 'UK')

2. **kwargs(Variable length keyword arguments)

**kwargs is similar to *args but just with one difference. Let us try to understand with an example.

Consider that we have a function customer_details where we want to pass multiple arguments, but we are not sure how many arguments these can be, we are also not sure about the order in which they can be, so while processing those arguments it can be a mess because we don’t know that what each argument mean.

customer_details(“Tom”,”Cruise”,35,’USA’)

customer_details(“Will”,32,”USA”)

Now while processing these arguments it is going to be challenging that what does each argument means.

To fix this issue we use kwargs which stands for keyword arguments. Here we will define what does each argument means while passing to a function and in function, these will be treated as a dictionary. Here also “kwargs” is not a reserved keyword, we can use any name, we are just supposed to use a double asterisk(**) before the name.

Function with customer details as kwargs. It will return us dictionary.

In [1]:
def cust_details(**kwargs):
    print(kwargs)
In [2]:
cust_details(first_name="Steve",lasy_name="Grant",id=2680,Country='USA')
{'first_name': 'Steve', 'lasy_name': 'Grant', 'id': 2680, 'Country': 'USA'}

Fetching values of dictionary.

In [3]:
def cust_details_value(**kwargs):
    print("Last Name =",kwargs['last_name'])
In [4]:
cust_details_value(first_name="Steve",last_name="Grant",id=2680,Country='USA')
Last Name = Grant

Function using random name other than kwargs.

In [5]:
def emp_details(**employee):
    print("Last Name =",employee['last_name'])
In [6]:
emp_details(first_name="Steve",last_name="Grant",id=2680,Country='USA')
Last Name = Grant

Parameter Vs Argument

The term parameter and argument are often used interchangeably when it comes to passing values to functions. Let us understand few differences between them:

Parameters Arguments
Parameters are also called Formal Parameters.Arguments are also known as Actual Parameters.
A parameter is a variable defined in a function definition.An argument is a value passed during the function call.
The parameters are local variables that are associated with the arguments.Each argument is associated with the parameter in the function definition.
These are used to receive data in a function.It is used to send data to a function.
Function Arguments. Parameter Vs Argument.

References

  1. Difference Between Argument and Parameter 
  2. Types of Arguments