Please Like, Share and Subscribe our YouTube Channel :
[Link]
For Video Explanation of this topic, please click on
the following link:
[Link]
FUNCTIONS IN PYTHON
Creating a User defined Function...
def Greet():
print("Welcome")
Greet()
A function header must contain atleast one statement
to be executed.. This will generate an error..
def Greet():
Greet()
Program to add two numbers using UDF (User defined
function).
def add():
num1=int(input("Enter first number:"))
num2=int(input("Enter second number:"))
Sum=num1+num2
print("Addition=",Sum)
add()
Variations in Functions
[Link] arguments and no return value..
def add():
num1=int(input("Enter first number:"))
num2=int(input("Enter second number:"))
Sum=num1+num2
print("Addition=",Sum)
add()
Please /swatichawlaofficial /swatichawlaofficial Prepared By :
Follow
us at: [Link]/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
[Link]
2. Functions with argument and no return value.
def add(a,b):
Sum=a+b
print("Addition=",Sum)
num1=int(input("Enter first number:"))
num2=int(input("Enter second number:"))
add(num1,num2)
[Link] with argument and return value..
def add(a,b):
Sum=a+b
return Sum
num1=int(input("Enter first number:"))
num2=int(input("Enter second number:"))
print("Addition=",add(num1,num2))
c=add(num1,num2)
print("Addition=",c)
Or
We can also write the following statement in place of
last two statements.
print("Addition=",add(num1,num2))
4. Functions with arguments and no return value.
def add(a,b):
Sum=a+b
print("Addition=",Sum)
num1=int(input("Enter first number:"))
num2=int(input("Enter second number:"))
c=add(num1,num2)
'''
If a function does not return any value, by default
it returns None.
'''
def Greet():
print("Welcome")
return
Please /swatichawlaofficial /swatichawlaofficial Prepared By :
Follow
us at: [Link]/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
[Link]
a=Greet()
print(a)
'''
The return statement ends the function execution even
if it is in
the middle of the function. The statements written
after return
statement will not get executed.
'''
def Greet():
print("Welcome")
return
print("Hello")
Greet()
'''
A function can return more than one value at a time,
in the form of tuple.
'''
def Operations(a,b):
add=a+b
sub=a-b
mul=a*b
div=a/b
return add,sub,mul,div
num1=int(input("Enter first number:"))
num2=int(input("Enter second number:"))
Result=Operations(num1,num2)
print(Result)
print(type(Result))
def Operations(a,b):
add=a+b
sub=a-b
mul=a*b
div=a/b
return add,sub,mul,div
Please /swatichawlaofficial /swatichawlaofficial Prepared By :
Follow
us at: [Link]/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
[Link]
num1=int(input("Enter first number:"))
num2=int(input("Enter second number:"))
s,d,m,d1=Operations(num1,num2)
print(s,d,m,d1)
print(type(s),type(d),type(m),type(d1))
Using Positional Argument..
def Simple_Interest(p,r,t):
print("Principle=",p)
print("Rate=",r)
print("Time=",t)
si=(p*r*t)/100
print("Simple Interest=",si)
Principle=1000
Rate=5
Time=2
Simple_Interest(Principle,Rate,Time)
Using Default Argument...
def Simple_Interest(p,r,t=3):
print("Principle=",p)
print("Rate=",r)
print("Time=",t)
si=(p*r*t)/100
print("Simple Interest=",si)
Principle=1000
Rate=5
Time=2
Simple_Interest(Principle,Rate,Time)
def Simple_Interest(p,t,r=7):
print("Principle=",p)
print("Rate=",r)
print("Time=",t)
si=(p*r*t)/100
print("Simple Interest=",si)
Principle=1000
Rate=5
Please /swatichawlaofficial /swatichawlaofficial Prepared By :
Follow
us at: [Link]/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
[Link]
Time=2
Simple_Interest(Principle,Time)
def Simple_Interest(p=2000,r=4,t=7):
print("Principle=",p)
print("Rate=",r)
print("Time=",t)
si=(p*r*t)/100
print("Simple Interest=",si)
Principle=1000
Rate=5
Time=2
Simple_Interest()
Scope of a Variable...
def Func():
a=10 # a is a local variable.
print(a)
Func()
a=10 # a is a global variable
def Func():
print("Inside func",a)
Func()
print("Outside function=",a)
a=10 # a is a global variable
def Func():
a=20 # a is a local variable
print("Inside func",a)
Func()
print("Outside function=",a)
Please /swatichawlaofficial /swatichawlaofficial Prepared By :
Follow
us at: [Link]/swatichawla12cs /swatichawlaofficial Swati Chawla
Please Like, Share and Subscribe our YouTube Channel :
[Link]
A=50
def func():
global A
A=A+10
print("Inside function",A)
print("Before Function Call",A)
func()
print("After Function Call",A)
To get access to Premium Notes, please join the
membership of the channel.
[Link]
fJELF8acGfBdw/join
Please /swatichawlaofficial /swatichawlaofficial Prepared By :
Follow
us at: [Link]/swatichawla12cs /swatichawlaofficial Swati Chawla