Python Function¶
- A function is a block of code which only runs when it is called.
- You can pass data, known as parameters, into a function.
- A function can return data as a resul
- A colon (:) to mark the end of the function header.
- An optional return statement to return a value from the function.
Creating a Function¶
- In Python a function is defined using the def keyword:
In [1]:
def function():
print("Hello, Welcome to MachineLearning.org.in")
function()
In [2]:
function()
Scope of a Variable in Function¶
Local variable is declared inside a function whereas Global variable is declared outside the function. Local variables are created when the function has started execution and is lost when the function terminates, on the other hand, Global variable is created as execution starts and is lost when the program ends.
In [3]:
a=10
def fun():
print("Inside:",a)
print("Outside:",a)
fun()
Note: Gloal variable can be used anywhere
In [4]:
a=10 #Local Variable
def fun():
a=20 #Global Variable
print("Inside:",a)
fun()
print("Outside:",a)
Note: Inside local function, local variable priority is first
In [5]:
a=10
def fun():
a=a+30
print("Inside:",a)
fun()
print("Outside:",a)
Note:: We can’not change global variable value inside a local function directly
In [6]:
a=10
def fun():
global a
a=a+30
print("Inside:",a)
fun()
print("Outside:",a)
Types of Functions¶
- Function with No Argument, No Return Value
- Function with No Argument, with Return Value
- Function with Argument, No Return Value
- Function with Argument, with Return Value
1. Function with No Argument, No Return Value¶
In [7]:
def add():
a=int(input("Enter First No:"))
b=int(input("Enter Second No:"))
print("Sum:",a+b)
add()
2. Function with No Argument, with Return Value¶
In [8]:
def add():
a=int(input("Enter First No:"))
b=int(input("Enter Second No:"))
print("Sum:",a+b)
a=add()
print(a)
In [9]:
def add():
a=int(input("Enter First No:"))
b=int(input("Enter Second No:"))
return a+b
a=add()
print("Sum:",a)
3. Function with Argument, No Return Value¶
In [10]:
def add(x,y):
print("Sum:",x+y)
a=int(input("Enter First No:"))
b=int(input("Enter Second No:"))
add(a,b)
4. Function with Argument, With Return Value¶
In [11]:
def add(x,y):
return x+y
a=int(input("Enter First No:"))
b=int(input("Enter Second No:"))
print("Sum:",add(a,b))
Default Argument¶
In [12]:
def fun1(a="itronix",b=20):
print(a,b)
fun1() #No Argument pass
fun1(b=30) #Single Argument Pass
fun1(b=30,a="sol") #Both Argument Pass
Calculate time of execution of a function¶
In [13]:
import time
def square():
start=time.time()
for i in range(100000000):
i=i*i
end=time.time()
print("Time Taken:",end-start)
square()
Pass Function as a Argument in other function¶
In [14]:
def fun(a):
a("Itronix Solutions")
fun(print)
Return Function from another Function¶
In [15]:
def fun():
return print
a=fun()
a("Machine Learning")
Nested Function¶
In [16]:
def fun1():
print("Hello")
def fun2():
print("Machine Learning")
fun2()
fun1()
Calculate the time of execution of each function¶
In [17]:
import time
def fun1(fun):
def fun2():
s=time.time()
fun()
e=time.time()
print("Time Taken:",e-s)
return fun2
def square():
for i in range(10000000):
i=i*i
def cube():
for i in range(10000000):
i=i*i*i
a=fun1(square)
a()
a=fun1(cube)
a()
Function Decorators : Time of execution of each function¶
In [18]:
import time
def fun1(fun):
def fun2():
s=time.time()
fun()
e=time.time()
print("Time Taken:",e-s)
return fun2
@fun1
def square():
for i in range(10000000):
i=i*i
@fun1
def cube():
for i in range(10000000):
i=i*i*i
square()
cube()