Python Forum
Question on "define function"; difference between return and print
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question on "define function"; difference between return and print
#1
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")
Reply
#2
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
Reply
#3
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.
Reply
#4
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).
Reply
#5
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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#6
(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.
Reply
#7
Yes, I changed my post. I'm sorry for the careless mistake
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#8
(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.
Reply
#9
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.
Reply
#10
Forum thread [Basic] functions
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  name.split() + (And) + print question sklord 12 3,720 Mar-26-2022, 05:45 PM
Last Post: deanhystad
  Use of function/return Paulman 6 2,372 Oct-24-2021, 11:07 PM
Last Post: Paulman
  Multiple return from function Grimmar 7 3,570 Mar-22-2021, 09:20 PM
Last Post: Grimmar
  Why does this function print empty list? hhydration 1 1,540 Oct-28-2020, 02:03 AM
Last Post: deanhystad
  Lambda function not return value mbilalshafiq 4 3,324 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,230 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  [split] problem with function return value ops 1 3,357 Apr-13-2020, 01:48 PM
Last Post: buran
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,553 Feb-27-2020, 09:22 AM
Last Post: buran
  Function to return today's month, day, and year sbabu 9 4,933 Jan-28-2020, 06:20 PM
Last Post: snippsat
  Function question JimBob9432 2 2,442 Oct-02-2019, 02:49 AM
Last Post: JimBob9432

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020