Python Forum
Question on "define function"; difference between return and print - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Question on "define function"; difference between return and print (/thread-27444.html)

Pages: 1 2


Question on "define function"; difference between return and print - extricate - Jun-07-2020

Hi there,

Can someone enlighten me on the diff between print and return when defining function. I get the same output for both, albeit with the apostrophe. Thanks alot

def yell_it(phrase):
    print(phrase)
    
    
yell_it("Hi")
def yell_it(phrase):
    return phrase
    
yell_it("Hi")



RE: Question on "def, difference between return and print - ndc85430 - Jun-07-2020

They are not the same thing at all. return returns the value to the calling function, so that it can be used in further computation. print, well, prints the result to the console and doesn't return its value (in fact it returns None, as do functions without an explicit return statement).

>>> def add(x, y):
...     return x + y
... 
>>> z = add(3, 4) * 2



RE: Question on "define function"; difference between return and print - Yoriz - Jun-07-2020

If a function does not have a return it defaults to returning None.
If you wanted to do something other than print the result and you did not return it you wouldn't be able to.


RE: Question on "define function"; difference between return and print - ndc85430 - Jun-07-2020

Functions that return values (i.e. other than those implicitly returning None like print) are more like functions in mathematics: they can be combined to produce new values. See, for example this image (sorry for the blurriness). The function f(x) takes an input (here x = 3), produces an output of 6 and then that input is used as the output to the function g(x) to produce a final value of 36. When one uses a function like print, you have no access to the value to be used later in your program; it's just printed to the screen (of course you could put the string in a variable or something, but that's besides the point here since we're talking about returns).


RE: Question on "define function"; difference between return and print - pyzyx3qwerty - Jun-07-2020

print() inside a function results in printing the result, whereas return() returns
the value. However, in either way, you need to have print() statement for the computer to show the value


RE: Question on "define function"; difference between return and print - ndc85430 - Jun-08-2020

(Jun-07-2020, 09:48 AM)pyzyx3qwerty Wrote: return() prints the value.

This is wrong. return doesn't print anything; it returns the value to the calling function.


RE: Question on "define function"; difference between return and print - pyzyx3qwerty - Jun-08-2020

Yes, I changed my post. I'm sorry for the careless mistake


RE: Question on "define function"; difference between return and print - extricate - Jun-08-2020

(Jun-07-2020, 07:37 AM)ndc85430 Wrote: Functions that return values (i.e. other than those implicitly returning None like print) are more like functions in mathematics: they can be combined to produce new values. See, for example this image (sorry for the blurriness). The function f(x) takes an input (here x = 3), produces an output of 6 and then that input is used as the output to the function g(x) to produce a final value of 36. When one uses a function like print, you have no access to the value to be used later in your program; it's just printed to the screen (of course you could put the string in a variable or something, but that's besides the point here since we're talking about returns).

Thanks alot for the explanation. I understand it now with the function example like in mathematics.

Do you have some examples of def function examples? The ones in the course are limited.


RE: Question on "define function"; difference between return and print - ndc85430 - Jun-08-2020

They're just called functions, not "def functions". I mean, what are you after really? I haven't got too much time to put a whole bunch of examples together - as long as you understand how to declare functions (with parameters where necessary) and call them, you should be OK. You might look at various threads in this forum as well or the source of any Python library, though both are likely to have problems (the former may not do things in the right way, the latter may be complicated for a beginner). It's worth trying to write your own functions, too.


RE: Question on "define function"; difference between return and print - Yoriz - Jun-08-2020

Forum thread [Basic] functions