+ 2

What is difference between returning sth and printing it?

For example, what is difference between A and B: A) def sum(x,y): return x+y B) def sum(x,y): print(x+y) Is B correct? As you know print is a function by itself. Why uaing print in this way seems nonsense?

18th Aug 2019, 1:24 AM
Mohsen Hazrati Yadkouri
Mohsen Hazrati Yadkouri - avatar
2 odpowiedzi
+ 5
When you return something, that something you return can be used for other means than simply to be displayed on screen, immediately or later. But when you go print() with it, the something is immediately displayed on screen, however since nothing is returned, the something is only known to you, others have no idea what it is, and hence can't do anything with it. Example A: You give me flour, yeast, eggs, I bake a cake from it, then return it to you. You can afterwards, do as you please with it, just show it to your friends, sell it, or eat it. You can even make it a giveaway or just throw it into the trash can. Example B: You give me flour, yeast, eggs, I bake something and I put it on a display for everyone to see. But I don't return the cake to you, you have no control of the cake even though you had passed me the ingredients. Your loss :D Q: "Why using print in this way seems nonsense?" A: Are you happy giving me the ingredients not getting back a cake in return? If you're OK with it, then print it is
18th Aug 2019, 1:41 AM
Ipang
+ 1
:D thanks to you I got the answer :)
18th Aug 2019, 1:46 AM
Mohsen Hazrati Yadkouri
Mohsen Hazrati Yadkouri - avatar