Defining Functions in python II :
Types of argument :
We have already discussed in previous section the technique to define and call a function, In
in this article we will discussed about some more ways of defining and calling function which will make python a wonderful language . some of these features includes ….
1.Position argument / Required argument2.Keyword argument
3.Default argument
4.Variable length argument
5.Keyworded Variable length argument
1.Position argument / Required argument:
When we call a function with some values, the values are assigned to the arguments based
on there position We are already using this types of argument in our function , In position /required arguments,the arguments are passed to a function in correct positional order also ,the number of arguments are passed to a function must be same as arguments mention while defining a function .
e.g.
def multi(a , b):
c=a*b
print(c)
multi(5,6)
OUTPUT = 30
In above case even if position of arguments are changed, It will not affect the output
But this will not happened in all cases ,If user define a function some where else and
forget the positions of argument then it will create difficulties to pass arguments , to
overcome this Python have an another type of argument Keyword argument
2.Keyword argument:
Python allows functions to be called using keywords where the position of arguments
can be changed. The values are not assigned to arguments according to there position
but based on there name . Keyword used in function calls, helps the function to identify the arguments by the parameter name .
e.g.
def student(Name , Roll_no):
print(Name)
print(Roll_no +10)
student (Name = 'Rahul' , Roll_no = 10)
output :
3.Default arguments :
Python allow user to specify function argument that can have default values that means it will be use when function is called with fewer element than it is defined to have that is ,if function have three parameter ,but function call by passing only two parameter ,then the third parameter will be assign the default values (already define ) .
The default value to an argument is assigned through an assignment operator " = ".
Example :
def show (department = IT ,name)
print("Department ",department)
print("Name ",name )
show(department =CS, name =Prashant)
show (name = Aditya )
Output :
Department CS
Name Prashant
Department IT
Name Aditya
4.Variable length arguments / Arbitrary Arguments :
Variable length arguments is used when number of arguments are not known to user
/ it is not known how many arguments will passed to a function .Python allows programmers to make a function call with arbitrary number of arguments or n number of arguments .
The syntax to use arbitrary arguments / variable length argument is
def function name(*variable name ):
# function body
EXAMPLE:
def multi(*a):
y=1
for i in a :
y = y * i
print(y)
multi(10,12,13,14)
OUTPUT : 21840
Above example is helpful while building a software for calculator where the number of
arguments are depends on user .
arguments are depends on user .
5.Keyworded Variable length argument:
This is just a mixed up of Keyword argument and Variable length argument .
In Variable length argument we just pass n number of arguments but what if user wants
to specify the variable name as well , for that python provides a another method to pass an
arguments i.e. Keyworded Variable length argument
syntax :
def function name(**variable name)
# function body
"* *" means user passing multiple arguments with keyword .
EXAMPLE :
def student(**information):
print(information)
student(name ="Aniruddha", Roll no =555, Mob no=5612347897)
OUTPUT :
{'name': 'Aniruddha', 'Roll no': 555, 'Mob no': 5612347897}
I hope this article on ways of passing argument is helpful for U …
Thank u .😊