When should I use "Return" or "print" in a function?

Asked

Viewed 1,808 times

-1

Can they exemplify some situation that can be exercised by one command but not another? Because here you gave the same thing in both commands.

Using print:

>>> def media(x1, x2):
...     print((x1+x2)/2)
>>> x = media(1, 3)
2

Using return:

>>> def medya(y1, y2):
...     return ((y1+y2)/2)
>>> y = medya(1, 3)
>>> y
2
  • 1

    It is not the same thing. Try to check the value of x for you to see.

  • @Andersoncarloswoss that ended up giving me one more question. What I got was the following: >> print(x) None >>> print(y) 5.0 ?

3 answers

5

It is not the same thing. In your example it seems to be, but it is because it is not a good example.

If you dig a little deeper, you’ll see that when used print, although the correct value is displayed, the value of x that receives the return function will be null, None.

>>> def media(a, b):
...     print((a+b)/2)
>>> x = media(1, 3)
2
>>> print(x)
None

That is, when using the print the result is sent to buffer output and then discarded. The value x, that would receive the result, receives a null value, because the function has no return.

Different if you use the return, because the value will be returned by the function and assigned in x:

>>> def media(a, b):
...     return (a+b)/2
>>> x = media(1, 3)
>>> print(x)
2

Note that even calling the function, the output is not displayed until the print(x), because the function sends the return to x and no longer for the buffer outgoing.

Imagine that you need to add 5 on average between 1 and 3. Using print you won’t be able to do x+5, for x is null, since the function had no return - and it is not possible to add 5 to null. Already using the return, the result 2 will be stored in x and thus it will be possible to add 5.

Ideally, a function should not use print, unless this is explicitly its goal: to display the data. A function that calculates the media should not have the responsibility of flaunt the average. This breaks the principle of sole responsibility and leaves your code barely reusable.

When you use the return, you will have direct access and power to manipulate the function return, which generates a much more versatile and malleable code for you.

Imagine the following problem:

  • Add the average between 1 and 3 with the average between 7 and 9.

Using the print, the first average result, 2, will be displayed on the screen as well as the second average result, 8. You will need someone to manually sum up 2 and 8 to get the result 10.

When you use the return, you can access the results within the program yet:

>>> x = media(1, 3)
>>> y = media(7, 9)
>>> x+y
10

You don’t need to know what the partial results are to get the total; if by chance you need to change media(7, 9) for media(7, 11), the result x+y will automatically become 11.

0

I chose to answer you in the simplest possible way by ignoring all the formalities:

Although they look the same, they are totally different and are used in completely different contexts.

The function print is used to print a given information on the console (terminal) where you are programming. Note: print means impression in English.

As to function return, returns some information from "inside" any function. Note: Return means return in English

This coincidence happens in python, but in java for example you would not be able to see the impression of a Return as it was done without a "print function".

I hope you understand!

0

Your confusion comes from the python suit, at command prompt (REPL) >>>, automatically print the returned value.

This does not normally occur, only when you are executing commands at the immediate python prompt - it is a matter of convenience - to facilitate the use of the immediate command interpreter, has a print() embedded that prints the return value.

In a true program, where you write the code in a file and command the execution of it, only the print() generates screen output. The return will not generate any output in this case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.