Python Functions Tutorial | Types of function parameters in Python

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




Types of function parameters in Python

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

Click here for Quiz on Functions in Python

1. Positional parameters in Python

A positional parameter is any parameter that’s not supplied as a key=value pair.

Value of first actual parameter goes to first formal parameter, second actual parameter goes to second formal parameter and so on as per their positions.

In the following program, formal parameter a gets 25 and formal parameter b gets 10.

Program to demonstrate positional parameters
def show(a,b):
     print(a,b)

show(25,10) 

#a gets 25 and b gets 10 
Output
25 10

2. Passing string as function parameter in Python

A string can be passed as a parameter to a Python function as like other parameters. We simplay need to pass string value as actual parameter while calling the function.

When we pass a string as argument, it is automatically passed by value. Strings are immutable so no changes can be made to formal parameter.

Program of function with string as parameter.
def show(var1):
     print(var1)
show('Amit')

”’ Amit’ will be stored in formal parameter var1 and shown within function body.”

Output
Amit




3. Passing list as function parameter in Python

A list can also be passed as a parameter to a Python function. We simply need to specify the name of list both as actual as well as formal parameter. There is no need to specify brackets.

When we pass a list as an argument, it is automatically passed by reference. Lists are mutable so changes be made to elements of list in function body will direct affect actual parameters.

Example 1 – Program of function with list as parameter.
def show(list1):
     print(list1)

L1=[10,20,30,40]
show(L1)
Output
[10, 20, 30, 40]
In the above program, L1 is the list passed as actual argument to function show(), list1 is the formal parameter which will receive values of L1
Example 2- Program of function with list as parameter.
def show(list1):
     list1[0]='a'

L1=[10,20,30,40]
show(L1)
print(L1) 
Output
['a', 20, 30, 40]
In the above program, L1 is the list passed as actual argument to function show(), list1 is the formal parameter which will receive values of L1.

First element of list1 i.e. list1[0] is assignmed ‘a’.

After the function call statement show(L1)

When we use print(L1) statement. It will display modified value of first element of list i..e changes made to list1 are reflected back to list L1.

Click here for Quiz on Functions in Python

4. Passing tuple as function parameter in Python

A tuple can also be passed as a parameter to a Python function. We simply need to specify the name of tuple both as actual as well as formal parameter. There is no need to specify parenthesis.

Tuples are immutable so we can’t make changes to tuple in formal argument.

Example – Program of function with tuple as parameter.
def show(tuple1):
     print(list1)

T1=[10,20,30,40]
show(T1)
Output
[10, 20, 30, 40]
In the above program, T1 is the list passed as actual argument to function show(), tuple1 is the formal parameter which will receive values of T1 .

5. Passing dictionary as function parameter in Python

A dictionary can also be passed as a parameter to a Python function. We simply need to specify the name of dictionary both as actual as well as formal parameter. There is no need to specify braces.

When we pass a list as an argument, it is automatically passed by reference. Lists are mutable so changes be made to elements of list in function body will direct affect actual parameters.

Program of function with list as parameter.
def show(dict1):
print(list1)D1={1:’a’,2:’b’,3:’c’}
print(D1)
Output
{1: 'a', 2: 'b', 3: 'c'}
In the above program, dictionary D1 is passed as actual argument to function show(), dict1 is the formal parameter which will receive values of D1.

6. Default parameters in Python

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

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

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

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



Example-1 : Program of function without default parameters.
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 parameters.
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

7. Required parameters in Python

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

Program of function with Required parameters.
def show(name,salary):
     print(name,salary)

show('Amit',50000)
Output
Amit 50000



8. Keyword arguments in Python

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

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

Program of function with Keyword parameters.
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 parameter age,
name will receive ‘Amit’ and
salary will recieve 50000.”’

Output
name= Amit
age= 30
salary= 50000

9. Variable number of parameters in Python

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

def func_name(var1, *vartuple)

Here var1 takes very first parameter value. All parameter values passed after first parameter 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  parameters.
def show(var1,*var2):
     print(num1)
     print(var2)
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)

Click here for Quiz on Functions in Python



Popular Books of Computer Science



Spread the love
Lesson tags: argument passing in python, default arguments in python, keywords arguments in python, passing arguments of a function in python, passing dictionary to function, passing lilst to function, passing string to function, passing tuple to function, postional parameters in python, required arguments in python, types of arguments in python, variable number of arguments in python
Back to: Python Programming Tutorial
Spread the love