Day4 PPT (Functions)
Day4 PPT (Functions)
to call a function
Functionname(arg1,arg2…)
Function function with args and return type
def addition(): def addition(a,b):
a=int(input("enter a : ")) return (a+b)
b=int(input("entrer b: ")) a=int(input("enter a : "))
c=a+b b=int(input("entrer b: "))
print("addition is : ", c) c=addition(a,b)
addition() print("addition is : ", c)
Types of function arguments
Positional args
Default args
Arbitary args
Keyword args
Arbitary keyword args
Positional args
Positional arguments are arguments that can be called by their position
in the function definition.
def addition(no1,no2):
c=no1+no2
print("addition is : ", c)
a=int(input("enter a : "))
b=int(input("entrer b: "))
addition(a,b)
Default args
Default values indicate that the function argument will take that value if no
argument value is passed during function call. The default value is
assigned by using assignment (=) operator.
def addition(no1,no2,no3=1):
c=no1+no2+no3
print("addition is : ", c)
a=int(input("enter a : "))
b=int(input("entrer b: "))
addition(a,b)
print("after replace default arg..")
addition(a,b,10)
Arbitary args
If you do not know how many keyword arguments that will be
passed into your function, add two asterisk
def addition(*a):
sum=0
for i in a:
sum=i+sum
print("Sum is : ",sum)
addition(1,2,3,4)
Keyword args
• You can also send arguments with the key = value syntax.
• This way the order of the arguments does not matter.
def addition(no1,no2,no3):
sum=no1+no2+no3
print("Sum is : ",sum)
addition(no1=1,no2=2,no3=3)
Arbitary keyword args
If you do not know how many keyword arguments that will be passed into
your function, add two asterisk: ** before the parameter name in the
function definition.
def addition(**a):
sum=0
i=0
for i in a
sum=sum+i
print("Sum is : ",sum)
addition(no1=1,no2=2,no3=3)
task1
Write a function program to return absolute number of a given number
Task 2
Write a program to Create a student details using function with keyword args
Details: student
name(string),id(int),mark1(int),mark2(int),mark3(int),mark4(int),mark5(int),total
and percentages
task1
def absolutevalue(num):
"""This function returns the absolute
value of the entered number"""
if num >= 0:
return num
else:
return -num
print(absolutevalue(no))
def studentdetails(name,m1,m2,m3,id1):
print("name :",name)
print("ID :",id1)
print("m1:",m1)
print("m2:",m2)
print("m3:",m3)
tot=(m1+m2+m3)
percent=((m1+m2+m3)*100)/300
print("Total:",tot)
print("Percentage :",percent)
name1=input("Enter the name:")
id11=int(input("Enter the ID :"))
m01=int(input("Enter the M1:"))
m02=int(input("Enter the M2:"))
m03=int(input("Enter the M3:"))
studentdetails(name=name1,id1=id11,m1=m01,m2=m02,m3=m03)