Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Function with arguments

Function with arguments receives values from calling


function (OR) function with arguments receives input.
Python allows writing a function with 4 types of
arguments.

1.Required positional arguments


2.Default arguments or optional arguments
3.Variable length arguments
4.Keyword arguments

Function arguments are local variable, which are used


within function. Memory of arguments allocated when
function is called and de-allocated after execution of
function.

Required arguments or required positional arguments


Required arguments required values at the time of
calling or invoking function. If the values are not
given to required arguments it generates error.

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

Python does not support pass by value, whenever function is


called by send object, it send address of object to
function.

If pass by reference if called function do some changes that


changes reflect to calling function.
Example:
def sort(l):
for i in range(len(l)):
for j in range(len(l)-1):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
def main():
n=int(input("Enter how many elements"))
list1=[int(input()) for i in range(n)]
print(f'before sorting {list1}')
sort(list1)
print(f'after sorting {list1}')
main()
Output:
Enter how many elements5
2
4
1
5
3
before sorting [2, 4, 1, 5, 3]
after sorting [1, 2, 3, 4, 5]

Example:
def fun1(s):
s[0]='P'
def main():
str1="python"
fun1(str1)
main()
Output:
TypeError: 'str' object does not support item
assignment

Only mutable objects are modified or updated in side


called function.

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

Function with default arguments or optional arguments


Function with default arguments

You might also like