In: Computer Science
How does a value-returning function differ from the void functions? in pyhton
NOTE :
INDENTATION IS ONE OF THE MAJOR ISSUE OF PYTHON PROGRAMMING. IF THERE IS ANY PROBLEM IN INDENTATION, KINDLY REFER TO THE SCREEN SHOTS OF THE PROGRAMS, BELOW THE TEXT BASED SOURCE CODES. AS SCREEN SHOT IS IMAGE BASED , SO THE INDENTATION IS SHOWN PERFECTLY IN SCREEN SHOT. IF REQUIRED, CAN REFER THE INDENTATION FROM SCREEN SHOTS AND MODIFY THE PROGRAM SOURCE CODES.THE CODES ARE GIVING EXACT OUTPUT.
Value returning function:
The function which returns any value, is called value returning function.
Void function:
The function which does not return any value is called void function
i. Syntax of function defining section of value returning function and void function are same in python.
ii. But the difference is :
Value returning function:
a. The value returning function of python returns value with keyword return.
b. The returned value is stored in called function and it can be used further.
Void function:
a. Void function does not return any value.
b. The calling function only calls the function. At function definition part entire operation is performed and result is generated.
Here in the example:
i. addition as value returning function, returns s, which is stored in res variable through calling function addition(lst) and print the value of res as final result.
ii. Addition in void type function in second program, calculate s and print the result of s in function definition part, where s is not returned in function calling part.
VALUE RETURNING FUNCTION :
# function retuns value
def addition(l):
s=0.0;
# add all values of list l
for i in range(0,len(l)):
s=s+l[i]
# returing value of s
return s
lst=[4,5,6,7,8,9,12,13,19];
# called function addition store the returned value of s of
addition(lst) function
#here addition(lst) is value returning function
res=addition(lst)
# print the result
print('Addition result is : ' ,res)
SCREEN SHOT
OUTPUT
VOID TYPE FUNCTION :
# function retun type is void
def addition(l):
s=0.0;
# add all values of list l
for i in range(0,len(l)):
s=s+l[i]
# print the result
print('Addition result is : ' ,s)
lst=[4,5,6,7,8,9,12,13,19];
#addition(lst) function is called here
# here addition(lst) is void function
addition(lst)
SCREEN SHOT
OUTPUT