In function definition there some arguments and they are called formal arguments. They are used to receive some value. When function is called some arguments are passed and they are called actual arguments.
The actual arguments are used in function call of four types:
Positional arguments
Keyword arguments
Default arguments
Variable length arguments.
'''positional arguments'''
These are the arguments passed in correct positional order. The number of arguments and their position in the function should match exactly.
Example:
def posargf(s1,s2):
s3=s1+s2
print(s3)
posargf("abc","def")
posargf("def","abc")
Output:
abcdef
defabc
Keyword arguments
These are identified by the name of the arguments. Here the order is not too important in function calling.
'''keyword arguments'''
def keyargf(rno,name):
print("Roll no is ",rno, " name is ",name)
keyargf(rno=1,name="def")
keyargf(name="abc",rno=2)
Output:
('Roll no is ', 1, ' name is ', 'def')
('Roll no is ', 2, ' name is ', 'abc')
Default arguments
Here in the function definition default value can be passed as parameter. So at the time of calling if value is omitted then it will consider the default value otherwise the passed value is considered.
Example:
def defargf(rno,name='***'):
print("Roll no is ",rno, " name is ",name)
defargf(1,"def")
defargf(2)
Osutput:
('Roll no is ', 1, ' name is ', 'def')
('Roll no is ', 2, ' name is ', '***')
Variable length arguments
When it is not known that how many arguments the function may receive, the variable length argument can be used. The variable length argument can accept any number of arguments.
The variable length argument written with’*’ symbol before it in function definitions. In following example frag is formal argument and *vargs is variable length argument.
'''variable length arguments'''
def variargf(farg,*vargs):
print("Formal argument",farg)
for i in vargs:
print("Variable argument",i)
variargf(1)
variargf(1,2,3,4)
variargf(1,"def")
Output:
('Formal argument', 1)
('Formal argument', 1)
('Variable argument', 2)
('Variable argument', 3)
('Variable argument', 4)
('Formal argument', 1)
('Variable argument', 'def')