+ 4

Can someone help and tell the difference between these two functions?

Is there a real difference between these two functions? def add_numbers(x, y): total = x + y return total print(add_numbers(4, 5)) def add_numbers2(z,q): total = z + q print(total) add_numbers2(6,7) To me, they look the same, just written differently.

18th Aug 2020, 8:51 AM
Ville Nordström
Ville Nordström - avatar
15 Antworten
+ 8
They are very much NOT the same one returns the total, the other just prints it. see how the first one, you need to explicitly call "print()" on while the bottom one you can just call the function? Keep messing around with them so you understand.
18th Aug 2020, 8:53 AM
Slick
Slick - avatar
+ 12
They are not the same , one return the value while the other function is void
18th Aug 2020, 7:59 PM
Ibrahim Babatunde Tijani
Ibrahim Babatunde Tijani - avatar
+ 5
Yes, its usually best to return values. when a function returns a value or values you can assign a variable name to the function and now the variable will equal whatever the function equals. printing the value is just showing a value, it cant do anything interactive with it. Not saying there no reason just to simply print the value, but you can always return it, then print what you return. YOU CANT do the opposite though.
18th Aug 2020, 9:00 AM
Slick
Slick - avatar
+ 4
Slick wait, so if I return the value, I could maybe do stuff like this?: Randomvariable = add_numbers(5, 8) And then I could use the Randomvariable like any other variable? Whereas, with the printing, I can't really do anything with it except for showing it?
18th Aug 2020, 9:06 AM
Ville Nordström
Ville Nordström - avatar
+ 4
Slick Ohh, thank you very much :)
18th Aug 2020, 9:09 AM
Ville Nordström
Ville Nordström - avatar
+ 3
Thats correct! Returning through functions like that makes life easier!
18th Aug 2020, 9:08 AM
Slick
Slick - avatar
+ 3
One prints and one returns this us the difference.return is more professional and more secured.
19th Aug 2020, 6:41 AM
David Sontu
David Sontu - avatar
+ 3
print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions. #codecademy
20th Aug 2020, 8:23 AM
arezou pakseresht
arezou pakseresht - avatar
+ 2
Only the 2nd returns an int ... it can be called by another function 🤔
19th Aug 2020, 2:55 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Yeah here it's a difference i can see... Is that... In first code if u assign function add_numbers to a variable. That variable will get the value returned by function.. But in second code if u assign this function to a variable it can't obtain any value bcz function doesn't return any value if u check it out the value of that variable by using print it will so none.
19th Aug 2020, 4:29 PM
saurabh
saurabh - avatar
+ 1
Slick is there a reason to use one over the other? It looks like the return argument(or command or something, what were these things called?) could make the code more compact even though the print one is probably more understandable for beginners like me.
18th Aug 2020, 8:56 AM
Ville Nordström
Ville Nordström - avatar
+ 1
You can also write this... x = f() ... if f doesn't return anything. However, x will then carry the object signifying nothing: None. (So normally there's little point doing that.)
18th Aug 2020, 11:36 AM
HonFu
HonFu - avatar
0
Ibrahim Babatunde Tijani what do you mean by void? Am I missing something you haven't seen me miss in the other replies?
19th Aug 2020, 5:03 AM
Ville Nordström
Ville Nordström - avatar
- 1
in add_numbers it will not print but in it will print
20th Aug 2020, 7:25 AM
priti gupta