Following example shows how function can be defined and called in python
'''write program to find sum of two numbers three times'''
def sum(a,b):
ans=a+b
print("Sum is ", ans)
i=0
while(i<3):
x=int(input("Enter first number"))
y=int(input("Enter second number"))
sum(x,y)
i=i+1;
Returning single value from function
''' write a program of function that returns single value through function'''
def sum(a,b):
ans=a+b
return ans
x=sum(2,3)
print("The return value is ", x)
Returning multiple value from function
The function can return multiple values through function in python. The function can return only single value in languages like C, C++ or JAVA. When the function returns multiple values, it is return as tuple.
'''write a program to interchange value of two variables'''
def swapnos(no1,no2):
t=no1
no1=no2
no2=t
return no1,no2
x=int(input("\nEnter any number"))
y=int(input("\n Enter any number"))
ans1,ans2=swapnos(x,y)
print("The values are", ans1, " " , ans2)
Define function
To perform particular task, if we write group of multiple statements, it is called function.
The multiple statements written in the function is called suite.
The function is part of original code.
If we write the original code in one section it is called function.
There are many advantages of function.
Advantages of function
The function provides many advantages.
It provides modularity.
with the use of function, we can write the main code in different sections that is functions, so it is easy to use the code anywhere in the program.
It provides reusability.
the function provides reusability, so if we have defined our function; we can call it multiple time without writing the same code again.
Error can be easily solved.
With use of function, the code is divided in different parts so, error can be easily solved. Particular function can be easily modified without disturbing the another function.
The length of program can be reduced.
once a function is defined, it can be called multiple time so rewriting the code can be avoided.
Difference between function and method
When the function is defined within class as a member it is called method.
The method can be called with use of object name of class name.
For example,
objectname.methodname()
classname.methodname()
Syntax of function
To define the function def keyword is used.
It is advisable to write in comment about the task of function within the definition of function.
The syntax is:
def functionname(argument1, argument2,…,argument)
‘’’ Write the task of function ‘’’
function definition statements
Calling a function
Function can not be invoked automatically.
It should be called explicitly.
So there are two important things related to function that is definition and calling
Function are first class object
Function are first class object so, task like function can return to variable, define a function inside another function, pass function as a parameter to another function and function return another function is possible:
Function can return to variable
'''function as objects'''
''' function assign to variable'''
def add(x,y):
return(x+y)
ans=add(3,4)
print("Ans is", ans)
Function can be define within another function
'''function as objects'''
''' Define function inside another function'''
def add(x,y):
def findsqr(p):
a=p*p
return(a)
return(findsqr(x)+findsqr(y))
ans=add(3,4)
print("Ans is", ans)
Pass function as a parameter to another function
'''function as objects'''
''' Define function as a parameter to another function'''
def add(x,y):
return(x+y)
def findsqr(p):
a=p*p
return(a)
ans=add(findsqr(3),findsqr(4))
print("Ans is", ans)
Function return another function
'''function as objects'''
'''function can return another function'''
def function1():
return('Function1')
def function2():
return(function1)
x=function2()
print(x())
======================================
Pass by object reference
In language like Java, when we pass value as parameter in function; there are two options:
Pass by value
Pass by reference.
In pass by value only value is passed so the modifications performed in function will not reflected outside of the function and in pass by reference the reference is passed so the changes will be reflected outside of the function.
In python neither of above concept is used . instead whenever anything(variable, list, tuple) passed , the value is passed in the form of object reference. Following program suggest this concept.
Example of single variable:
'''pass by object reference for integer value'''
def myfunction(x):
print("x value in function",x)
print("id of x in function",id(x))
x=15
print("x value after updating in function",x)
print("id of x after updating in function",id(x))
x=10
print("x value at calling",x)
print("id of x at calling",id(x))
myfunction(x)
print("x value after calling the function",x)
print("id of x after calling the function",id(x))
output:
('x value at calling', 10)
('id of x at calling', 21233236)
('x value in function', 10)
('id of x in function', 21233236)
('x value after updating in function', 15)
('id of x after updating in function', 21233176)
('x value after calling the function', 10)
('id of x after calling the function', 21233236)
Example of List
'''pass list as an argument in function'''
def myfunction(lst):
print("listvalue in function",lst)
print("id of list in function",id(lst))
lst.append(10)
print("list value after updating in function",lst)
print("id of list after updating in function",id(lst))
lst=[1,2,3,4,5,]
print("list value at calling place",lst)
print("id of list at calling place",id(lst))
myfunction(lst)
print("list value after calling the function",lst)
print("id of list after calling the function",id(lst))
output:
('list value at calling place', [1, 2, 3, 4, 5])
('id of list at calling place', 34624856)
('listvalue in function', [1, 2, 3, 4, 5])
('id of list in function', 34624856)
('list value after updating in function', [1, 2, 3, 4, 5, 10])
('id of list after updating in function', 34624856)
('list value after calling the function', [1, 2, 3, 4, 5, 10])
('id of list after calling the function', 34624856)
From above examples we can identify that, in python integer, floats, strings and tuples are immutable. That means they cannot be modified. When we try to change the value new object is created with the modified value.
List and Dictionary are mutable means we can modify its value. So when we change the value of list the same object is modified and we can get the changed value outside the function also.