2

Lets say I have the function:

def function(a)
    c = a+b
    print(c)

Is it advisable to use the print statement in the function to display output rather than placing a return statement at the end and using the print(function(a))?

Also what implications would there be if I used both a print statement and a return statement in a function to display the same output? Lets imagine I need to show the answer for c and then use the value of c somewhere else. Does this break any coding conventions?

So the highlight of the question isn't the difference between print and return, but rather if it is considered a good style to use both in the same function and if it has a possible impact on a program. For example in:

 def function(a)
    c = a+b
    print(c)
    return c
 value = function
 print(value)

Would the result be two c's? Assume c = 5; therefore, would the output be(?):

5
5
Saroekin
  • 1,175
  • 1
  • 7
  • 20
uhexos
  • 383
  • 4
  • 19
  • 1
    So what would happen if you wanted to reuse that function and did *not* want to print the result, but instead wanted to use the result as part of a new expression? – Martijn Pieters Nov 10 '15 at 21:11
  • My $0.02, for what it's worth. I work a lot on programs with deeply nested function calls. Putting the prints at the highest level possible is usually good, because the deeper they are, the more often they print, and usually the output gets cumbersome. Then you have to dig back through your code to find all the stray prints. Which leads to another tip: ALWAYS label a print statement, never just `print (function(a))`, instead `print "my function result = ", function(a)`. That also makes it much easier to remove them later because you have a literal string to search on. – gariepy Nov 10 '15 at 21:12
  • 1
    Depends entirely on your use case. Usually a `return` is used in case you want to read the result of the function into a variable and use it out of the function. In your example, the value of `c` will not be available outside the function (for any kind of computation). Also using both print and return is not logical as it will interfere with your usage of the return value – letsc Nov 10 '15 at 21:12
  • Possible duplicate of [Difference between returns and printing in python?](http://stackoverflow.com/questions/3881434/difference-between-returns-and-printing-in-python) – Pynchia Nov 10 '15 at 21:19
  • 1
    @letsc your last statement is incorrect. You can print and then return just fine. – MattDMo Nov 10 '15 at 21:19
  • its an easy misconception giving the fact that they could both be used to display the result. Someone could easily forget that where as print merely displays a value, return actually stores it to a variable @Jose – uhexos Nov 10 '15 at 21:30
  • I redacted my comment after OP edited his query. it is clear that OP doesn't suffer from the `print`/`return` delusion, but is asking a more subtle question. – Robᵩ Nov 10 '15 at 21:31
  • @uhexos: "return actually stores it to a variable" is an incorrect statement. If you do `return "foo"`, you have no way of determining whether `"foo"` gets stored in a variable or not. – Bryan Oakley Nov 10 '15 at 21:48
  • @bryan wouldn't foo get assigned as the value always returned by the function. – uhexos Nov 10 '15 at 21:55
  • @uhexos: the caller may simply throw the value away without assigning it, or pass it to another function without saving it to a variable. – Bryan Oakley Nov 10 '15 at 22:56

2 Answers2

6

print and return solve two completely different problems. They appear to do the same thing when running trivial examples interactively, but they are completely different.

If you indeed just want to print a result, use print. If you need the result for further calculation, use return. It's relatively rare to use both, except during a debugging phase where the print statements help see what's going on if you don't use a debugger.

As a rule of thumb I think it's good to avoid adding print statement in functions, unless the explicit purpose of the function is to print something out.

In all other cases, a function should return a value. The function (or person) that calls the function can then decide to print it, write it to a file, pass it to another function, etc.

So the highlight of the question isnt the difference between print and return but rather if it is considered good style to use both in the same function and its possible impact on a program.

It's not good style, it's not bad style. There is no significant impact on the program other than the fact you end up printing a lot of stuff that may not need to be printed.

If you need the function to both print and return a value, it's perfectly acceptable. In general, this sort of thing is rarely done in programming. It goes back to the concept of what the function is designed to do. If it's designed to print, there's usually no point in returning a value, and if it's designed to return a value, there's usually no point in printing since the caller can print it if it wants.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
1

Well return and print are entirely two different processes.

Whereas print will display information to the user or through the console; and return is used for collecting data from a method that fulfills a certain purpose (to use later on throughout your program).

And to answer your question, I believe it would return the two values; since one prints the c variable itself, and the other returns the value c to present as well? Correct me if I'm wrong.

Saroekin
  • 1,175
  • 1
  • 7
  • 20