Python Functions Tutorial | Types of function arguments in Python

Preview
You must first complete Python Functions Tutorial | Using Functions in Python 3 before viewing this Lesson

Types of function arguments in Python

In Python, we can take four different types of arguments in a user defined function. They are as follows:

1. Default arguments:

In default arguments, we can assign some value to the formal arguments of a function but the assignment of values is possible from last argument of the function towards left.

The main use of default argument is that we may or may not provide value for default argument while calling the function because function will automatically take the default value of formal argument in the function.

Example: def show(a=5,b=10)

In the above example, a is a default argument having value 5 and b is also a default argument having value 10.



Example-1 : Program of function without default arguments.
def show(a=5,b=10):
     print(a,b)
show() #a takes 5, b takes 10
show(25) #a takes 25, b takes 10
show(25,40) #a takes 25, b takes 40
Output
5 10
25 10
25 40

 

Example-2 Program of function without default arguments.
def display(fname,lname=”Kumar”):
     print(fname,lname)
display(“amit”) #fname gets ‘amit’, lname gets ‘Kumar’
display(“amit”,”singh”) #fname gets ‘amit’, lname gets ‘singh’
Output
amit Kumar
amit singh

2. Required arguments:

In case of Required arguments, number and order of actual arguments and formal arguments must be same. The values of actual arguments are assigned to formal arguments on one to one basis i.e. First actual argument in assigned to first formal argument, second actual argument in assigned to second formal argument and so on.

Program of function with Required arguments.
def show(name,salary):
     print(name,salary)
show(‘Amit’,50000)
Output
Amit 50000



3. Keyword arguments:

The keyword arguments are used to specify the names of formal arguments while calling the function. The main advantage of keywords arguments is that we can write actual arguments in any order we want.

Values of actual arguments will be passed to formal arguments on the basis of their names rather than their order.

Program of function with Keyword arguments.
def show(name,age,salary):
     print(“name=”,name)
     print(“age=”,age)
     print(“salary=”,salary)
show(age=30,salary=50000,name=’Amit’)
”’value of age will be 30 in formal argument age, name will receive ‘Amit’ and salary will recieve 50000.”’
Output
name= Amit
age= 30
salary= 50000

4. Variable number of arguments:

This technique of passing arguments to a function is very useful when we do not know the exact number of arguments that will be passed to a function. Syntax of function with variable number of arguments is:

def func_name(var1, *vartuple)

Here var1 takes very first argument value. All argument values passed after first argument will be stored in  *vartuple  in the form of a tuple. We can perform operations on these values just as we work with tuples.

Program of function with variable number of arguments.
def show(var1,*var2):
print(num1)
print(t)
show(‘Amit’,1,3,4,5,6)
” ‘Amit’ will be stored in argument var1. Values 1,3,4,5,6 will be stored as a tuple in var2.”’
Output
Amit
(1, 3, 4, 5, 6)




Lesson tags: argument passing in python, default arguments in python, keywords arguments in python, passing arguments of a function in python, required arguments in python, types of arguments in python, variable number of arguments in python
Back to: Python Programming Tutorial