Professional Documents
Culture Documents
Function With Arguments: Def Def Def
Function With Arguments: Def Def Def
Example:
def fun1(): # fun1 is without arguments
print("inside fun1")
def fun2(a,b): # fun2 is with 2 arguments
print(a,b)
def main():
fun1()
fun2(100,200)
fun2(a=100,b=200)
fun2(b=400,a=300)
main()
Output:
inside fun1
100 200
100 200
300 400
Example:
def display_dictioary(d): # function with req arg
for key,value in d.items():
print(f'{key}--->{value}')
def main():
course_dict={'java':2000,'python':4000,'c++':1000}
student_dict={'naresh':'python','suresh':'java','kishore':'c
'}
display_dictioary(student_dict)
display_dictioary(course_dict)
main()
Output:
naresh--->python
suresh--->java
kishore--->c
java--->2000
python--->4000
c++--->1000
Example:
def fun1(s):
s[0]='P'
def main():
str1="python"
fun1(str1)
main()
Output:
TypeError: 'str' object does not support item
assignment
return statement
“return” is a keyword
Return is a passes control statement
Return statement send/return value to calling function
After returning value it terminates execution of
function.
Example:
def add(a,b):
c=a+b
return c
def main():
res=add(100,200)
print(res)
main()
Output:
300
Example:
def fun1():
print("Hello")
return 100
print("Bye")
def main():
x=fun1()
print(x)
main()
Output:
Hello
100
Example:
def fun1():
return 10
return 20
return 30
def main():
x=fun1()
print(x)
main()
Output:
10
Example:
def fun1():
return 10,20,30,40,50
def main():
x=fun1()
print(x)
main()
Output:
(10, 20, 30, 40, 50)
Example:
def power(n,p):
r=n**p
return r
def maximum(n1,n2):
if n1>n2:
return n1
else:
return n2
def simple_interest(a,t,r):
return (a*t*r)/100
def main():
x=power(5,2)
print(x)
si=simple_interest(5000,12,1.5)
m1=maximum(100,200)
m2=maximum(400,200)
print(si,m1,m2)
main()
Output:
25
900.0 200 400
Example:
def string_upper(s):
s1=""
for ch in s:
if ch>='a' and ch<='z':
s1=s1+chr(ord(ch)-32)
else:
s1=s1+ch
return s1
def main():
str1=input("Enter any string")
str2=string_upper(str1)
print(str1)
print(str2)
main()
Output:
Enter any stringpython 3.9
python 3.9
PYTHON 3.9