PURE function in Python. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

PURE function in Python.

Some one explain me what is pure function in Python. Pls don't use complex terms to explain it.

2nd Sep 2017, 2:55 PM
Sampath Kumar
Sampath Kumar - avatar
3 Answers
+ 4
for example let's see this situation. a=2 b=3 c=0 def sum(a,b): c=a+b print(c sum(2,3) here in this situation the function changed value of a global variable c, and then did a extra thing of printing and in main program we called this function and it did the job, it's an impure function. we need a function that just accepts inputs , process the input and returns output and nothing else, we will use it's returned value in main program. a=2 b=3 c=0 def sum(n1,n2): n3=n1+n2 return n3 c=sum(2,3) print (c) here the function is pure function it doesn't disturb global variables or does not do anything else just takes input, process values and return, now if required we can ourselves do the changes to the global variables with the value it returns or call other functions in the main program. this approach is called functional programming where you arrive at a program that is constituted by different pure functions as different parts of program to do processing, and then use all of them together systematically. and we implement different functions to do different things.
2nd Sep 2017, 6:53 PM
Sandeep Chatterjee
+ 3
pure function in programming language is a function that may accept inputs, process them and produce a output, the output produced is unique for a particular set of arguments and it only returns output without any side effects that means it only takes input and return something, and doesn't change anything of the main program or modify global variables.
2nd Sep 2017, 3:38 PM
Sandeep Chatterjee
+ 2
Example pls.
2nd Sep 2017, 3:59 PM
Sampath Kumar
Sampath Kumar - avatar