Function Arguments In Python | FACE Prep

Functions in Python can be defined with or without parameters. If a function is defined with parameters, then we need to pass arguments to these parameters. So in simple, function arguments in Python are input values that are passed to the parameters of the function definition. 

Python has different types of arguments that can be passed to parameters of the function definition. Let us discuss these in detail.

Types of Function Arguments 

Python functions support multiple types of arguments such as 

  • Positional arguments
  • Keyword arguments
  • Default arguments
  • Variable-length positional arguments
  • Variable-length keyword arguments

a) Positional arguments

Positional arguments are assigned to the parameters of the function definition in sequential order. The first argument passed from the main() function is assigned to the first parameter of the function definition and so on. These types of arguments should be passed in the correct order in which they are declared in the function definition. Let us look at an example to understand better.

Consider the below-given function ‘employee’ which prints the details (name, age and salary) of employees. All these details passed from the main() function get assigned to the parameters of function definition in sequential order as shown below. 

#function definition which prints the details of an employee
def employee(name, age, salary):
print("Employee name:", name)
print("Employee age:", age)
print("Employee salary:", salary)
print()

#main() function which passes the employee details
employee("Jack", 22, 20000)
employee("Jhon", 23, 21000)
Output:
Employee name: Jack
Employee age: 22
Employee salary: 20000

Employee name: Jhon
Employee age: 23
Employee salary: 21000

User should be careful while passing these positional arguments. If the arguments are passed in the wrong order, then we will not end up with the desired output as shown below.

#function definition which prints the details of an employee
def employee(name, age, salary):
    print("Employee name:", name)
    print("Employee age:", age)
    print("Employee salary:", salary)
    print()

#main() function where the user enters the employee details in the wrong order
employee(22, "Jack", 20000)
employee(21000, 23, "Jhon")
Output:
Employee name: 22
Employee age: Jack
Employee salary: 20000

Employee name: 21000
Employee age: 23
Employee salary: Jhon

The disadvantage of positional arguments can be overcome by the next type of argument which is keyword arguments.

b) Keyword arguments

These arguments are passed using the names of their corresponding parameters to avoid the confusion created by positional arguments. In this case, the order of the arguments passed doesn’t matter, as the arguments are matched by name and not by position.

In the below-given example, since the arguments are passed with their respective parameters, we got the desired output even though the arguments are not passed in sequential order.

#function definition which prints the details of an employee
def employee(name, age, salary):
    print("Employee name:", name)
    print("Employee age:", age)
    print("Employee salary:", salary)
    print()
 
#main() functon in which details of the employee are passed along with their parameters name
employee(age = 22, name = "Jack", salary = 20000)
employee(salary = 21000, age = 23, name = "Jhon")
Output:
Employee name: Jack
Employee age: 22
Employee salary: 20000

Employee name: Jhon
Employee age: 23
Employee salary: 21000

But passing an argument along with its parameter name is pointless in the case of a single argument. 

For instance, consider a program to calculate the factorial of a number where the user needs to input only one argument. Since the function definition accepts a single argument, we can pass the argument without parameter name.

#program to print the factorial of a number
def fact(num):

    f = 1
    for i in range (1, num + 1):
        f = f * i 
    print("The factorial of", num , "is", f)
        
num = int(input())
fact(num)
Output:
The factorial of 5 is 120

Consider a function defined with three parameters. But, the user inputs only two arguments for that function definition. In this case, the next type of argument (default arguments) comes in handy.

c) Default arguments

Python allows us to initialize default arguments for the parameters when defining a function. These are used when arguments are not passed for the respective parameter from the mian() function. 

In the below-given code, since salary is not passed when the function is called for the first time, interpreter assigns the default argument (20000) when printing the employee details for the first time. 

Also, age is not passed when the function is called for the second time. So, interpreter assigns the default argument (23) when printing the employee details for the second time. 

#function definition with default arguments
def employee(name, age = 23, salary = 20000):
    print("Employee name:", name)
    print("Employee age:", age)
    print("Employee salary:", salary)
    print()

employee(age = 22, name = "Jack")
employee(salary = 21000, name = "Jhon")
Output:
Employee name: Jack
Employee age: 22
Employee salary: 20000

Employee name: Jhon
Employee age: 23
Employee salary: 21000

The last type of argument is variable-length arguments.

d) Variable-length arguments

These arguments are used when the number of arguments to be passed to the function is not known beforehand. There are two types of variable-length arguments namely variable-length positional arguments and variable-length keyword arguments.

i) Variable-length positional arguments (*args)

When you prefix a parameter with an asterisk (*) symbol, it collects all the positional arguments and stores them in the form of a tuple. The below-given function prints all the arguments passed to the function in the form of a tuple.

def var_args(*args):
    print(args)
var_args("FACEPrep", "Python", 30, 29.56)
Output:
('FACEPrep', 'Python', 30, 29.56)
ii) Variable-length keyword arguments (**kwargs)

**kwargs works only for keyword arguments. It collects all the keyword arguments into a dictionary, where the argument names are the keys, and arguments are the corresponding dictionary values.

In the below-given code, ‘website’, ‘article’, ‘days’ are the keys and ‘FACE Prep’, ‘Python’, ’60’ are the values passed to the function definition.

def var_args(**kwargs):
    print(kwargs)
var_args(website = "FACEPrep", article = "Python", days = 60)
Output:
{'article': 'Python', 'days': 60, 'website': 'FACEPrep'}

Function arguments FAQs

In Python, we have five types of function arguments namely positional, keyword, default, variable-length positional and variable-length keyword arguments.

Function parameters are the variables in a method definition. It collects and stores the arguments passed from the main() function.