Function Arguments and Return Statements
'''
Function Argument and Return Statements:
There are four types of arguments that we can provide in a function:
1.Default Arguments
2.Keyword Arguments
3.Variable-Length Arguments
4.Required Arguments
'''
def average(a, b):
print("The average is", (a+b) /2)
average(4, 6) #output = The average is 5.0
'''
Default Argument:
This is a function parameter that has a default value assigned to it i.e. if the programmer does not provide a value for that parameter, the default value is used instead.
'''
def average(a=10, b=20):
print("The average is", (a+b)/2)
average( ) #output = The average is 15.0
#Here the default values of a, b are used as we have not mentioned any specific values for the parameters.
def average(a=10, b=20):
print("The average is", (a+b)/2)
average(12, 22) #output = The average is 17.0
#Here the values of a, b are already specified. Hence, the default values were not considered.
def average(a=10, b=20):
print("The average is", (a+b)/2)
average(a=11) #output = The average is 15.5
average(b=25) #output = The average is 17.5
#If we specify the value of only one parameter, the other one will consider the default value.
def name(fname, mname = "Kumar", lname= "Choudhury" ):
print("Hello", fname, mname, lname)
name("Prasad") #output = Hello Prasad Kumar Choudhury
'''
Keyword Argument:
We can provide arguments with key(value), this way the interpreter recognizes the arguments by the parameter name. Hence, the order in which the arguments are passed, does not matter.
'''
def name(fname, mname, lname):
print("Hello", fname, mname, lname)
name(lname = "Choudhury", fname = "Prasad", mname = "Kumar")
#output = Hello Prasad Kumar Choudhury
'''
Required Argument:
This is an argument that must be passed to a function when it is called. If it is not provided, Python will raise a "Typeerror".
This is also called Positional Argument.
'''
def average(a, b, c=3):
print("The average is", (a+b+c)/2)
average(a=1, b=2) #output = The average is 3.
#Here the values of a, b are required arguments and value of c is optional.
#average() #output = Typeerror
'''
Variable-Length Argument:
If we need to pass more arguments than those defined in the actual function, this can be done using variable-length arguments.
'''
def average(*numbers):
sum = 0
for i in numbers:
sum = sum + i
print("Average is: " , sum / len(numbers))
print(type(numbers)) #output = <class 'tuple'>
average(24, 9) #output = Average is: 16.5
def name(**name):
print("Hello", name["fname"], name["mname"], name["lname"])
print(type(name)) #output = <class 'dict'>
name(mname = "Kumar", fname = "Prasad", lname = "Choudhury")
#output = Hello Prasad Kumar Choudhury
'''
Return Statement:
This is used to return the value of the expression back to the calling function.
'''
def average(*numbers):
sum = 0
for i in numbers:
sum = sum + i
return sum/len(numbers)
c = average(1, 2, 3, 4)
print(c) #output = 2.5
def average(*numbers):
sum = 0
for i in numbers:
sum = sum + i
return 10
return sum/len(numbers)
c = average(1, 2, 3, 4)
print(c) #output = 10
#The first value of return will be considered.
Comments
Post a Comment