r/eli5_programming • • Sep 10 '22
What's the difference between a fruitful function and a void function?
I understand that adding a 'return' makes all the difference between either printing the two sums or getting a 'none' . What I want to understand here is why is the case like this please and how is a function void despite giving an output
Input: def add(x,y): print ('Sum is =', x + y) return_val = add(50,5) print (return_val) sum = add(20,10) print (sum)
Output: Sum is = 55 None Sum is = 30 None
Input: def add(x,y): print ('Sum is =', x + y) return x + y return_val = add(50,5) print (return_val) sum = add(20,10) print (sum)
Output: Sum is = 55 55 Sume is = 30 30
2
u/ralphtheowl Sep 11 '22
Printing the result is not the same as returning a result. Returning a value lets other parts of your code (ie whichever line that called the function) do something with the returned value. Printing a value does just that, and returns nothing.
1
u/ArtemonBruno Sep 11 '22 edited Sep 11 '22
Learning from your discovery of fruitful function vs void function.
What I got from this is, object can be function or values. print() only show value (but not functions contents). print() also not running the functions.
Edit: