You are on page 1of 5

funtions

1. darshan
2.blessy => 11th
3. josheph
4. cherimsh
5.parswanthan
6. seetha

functions
=> function is a subprogram that acts on a data
and it returns value.

syntax
def functionname(arg1,arg2,arg3):
statement1
statement2

note:
function defintion
def functionname(arg1,arg2,arg3): #function header
statement1 ----|
statement2 -----| body of statement

note:
def => keyword. it indicates function starts
function name => one of the identifiers
arg1,arg2,arg3=> necessity variable

1. write a python function to add two values using function


functionname:addition()
argments => a,b
formula => c=a+b

solution:
def addition(a,b):
c=a+b
print("c value=",c)
##function ends here
##main() function starts here
a=10
b=20
addition(a,b)

example
def addition(a,b): ## function header
c=a+b
print("c value=",c)
##function ends here
##main() function starts here
a=10
b=20
addition(a,b) ## function calling
p=50
q=30
addition(p,q)
addition(60,70)
"""
c value= 30
c value= 80
c value= 130

"""

output
def addition(a,b): ## function header
c=a+b
print("c value=",c)
a=10
b=20
c=30
addition(a) ## function def 2 arg mismatch
addition(a,b,c)## function def 2 arg mismatch with 3 arg

2. write a python function to find the simple interst


si=p*n*r/100

function name: findinterest()


argments:p,n,r
formula=si=p*n*r/100
solution:
def findinterest(p,n,r):
si=p*n*r/100
print("simple interest=",si)
p=int(input("enter the principle"))
n=int(input("enter no or year"))
r=int(input("enter rate of interest"))
findinterest(p,n,r)
"""
enter the principle5000
enter no or year1
enter rate of interest2
simple interest= 100.0

"""

3. write a python function to pass two arguments like a,b value


and swap two values
function name:swap()
arg:a,b
a=10
b=20
a=20 b=10

solution:
def swap(a,b):
a,b=b,a
print("a =",a,"b=",b)
a=10
b=20
swap(a,b)
output
a = 20 b= 10

4) write a program to find the sum of the following series


1+2+3+4+..............n

solution:
n=int(input("enter n value"))
sum=0
for i in range(1,n+1):
sum=sum+i
print("sum value=",sum)

5) write a python function sumseries()


to pass n value
and find the sum of series.
solution:
def sumseries(n):
sum=0
for i in range(1,n+1):
sum=sum+i
print("sum value=",sum)
n=int(input("enter n value"))
sumseries(n)

You might also like