+ 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.
15 Answers
+ 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.
+ 12
They are not the same , one return the value while the other function is void
+ 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.
+ 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?
+ 4
Slick Ohh, thank you very much :)
+ 3
Thats correct! Returning through functions like that makes life easier!
+ 3
One prints and one returns this us the difference.return is more professional and more secured.
+ 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
+ 2
Only the 2nd returns an int ... it can be called by another function 🤔
+ 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.
+ 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.
+ 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.)
0
Ibrahim Babatunde Tijani what do you mean by void? Am I missing something you haven't seen me miss in the other replies?
- 1
in add_numbers it will not print
but in it will print