Function Part2
Function Part2
1. Keyword-Only Arguments
Keyword-only arguments mean whenever we pass the arguments (or value) by their parameter names at the time of calling the function in Python in which if you change
the position of arguments then there will be no change in the output.
nameAge(name="Prince", age=20)
nameAge(age=20, name="Prince")
Output
Hi, I am Prince
My age is 20
Hi, I am Prince
My age is 20
2. Positional-Only Arguments
def nameAge(name, age):
print("Hi, I am", name)
print("My age is ", age)
Output
Case-1:
Hi, I am Prince
My age is 20
Case-2:
Hi, I am 20
My age is Prince
Example 2
def minus(a, b):
return a - b
a, b = 20, 10
result1 = minus(a, b)
print("Used Positional arguments:", result1)
result2 = minus(b, a)
print("Used Positional arguments:", result2)
Output
Used Positional arguments: 10
Used Positional arguments: -10
a = 1
b = 3
# arguments passed
n = no_of_argu(1, 2, 4, a)
# result printed
print(" The number of arguments are: ", n)
Output :
The number of arguments passed are: 4
Example 2:
Python3
def no_of_argu(*args):
print(no_of_argu(2, 5, 4))
print(no_of_argu(4, 5, 6, 5, 4, 4))
print(no_of_argu(3, 2, 32, 4, 4, 52, 1))
print(no_of_argu(1))
Output :
3
6
7
1
Python
def student(firstname, lastname ='Mark', standard ='Fifth'):
print(firstname, lastname, 'studies in', standard, 'Standard')
# 1 positional argument
student('John')
# 3 positional arguments
student('John', 'Gates', 'Seventh')
# 2 positional arguments
student('John', 'Gates')
student('John', 'Seventh')
Output:
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
print(shout('Hello'))
yell = shout
print(yell('Hello'))
Output:
HELLO
HELLO
var=func
var()
var()
Example:
Python
def a():
print("GFG")