Loading...
When I write a function I sometimes use print and sometimes return and both seem to show the answer. But my teacher says they are not the same. Why does it matter?
print only displays a value on screen for a human to read, it does not give the value back to the program. return sends a value back to whoever called the function so it can be stored or reused. Example: a function with 'return a + b' lets you write 'total = add(2, 3)' and total becomes 5, which you can use later. If the function used 'print(a + b)' instead, it shows 5 but 'total = add(2, 3)' makes total equal to None, because nothing was returned. Rule of thumb: use return when the result will be used further in the program, and print only when you simply want to show something. Functions that calculate should almost always return.
Sign in as a tutor to answer this doubt.