What's the difference between the print and the return functions?
And what does the return function actually mean, please?
Answer 51900c3fa31802c99c0043c6
Unfortunately, there is a character limit so this will be in many parts. First thing to note is that return
and print
are statements, not functions, but that is just semantics.
I’ll start with a basic explanation. print
just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return
is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.
On a more expansive note, print
will not in any way affect a function. It is simply there for the human user’s benefit. It is very useful for understanding how a program works and can be used in debugging to check various values in a program without interrupting the program.
return
is the main way that a function returns a value. All functions will return a value, and if there is no return
statement (or yield
but don’t worry about that yet), it will return None
. The value that is returned by a function can then be further used as an argument passed to another function, stored as a variable, or just printed for the benefit of the human user.
Consider these two programs:
def function_that_prints():
print "I printed"
def function_that_returns():
return "I returned"
f1 = function_that_prints()
f2 = function_that_returns()
print "Now let us see what the values of f1 and f2 are"
print f1
print f2
18 comments
“No one” Hush your gums. It’s not your fault that you’re unable to comprehend his explanation.
ikr
looks great :)
very helpful
Thanks Man You really helped me get it in the first couple lines
This most certainly helps. Thank you!
this sux I didn’t understand
Hello
Hi BaronGinger
hello jack, hows’ goin????
I’m fine thanks hows u
top notch spllin
top qual
thank you
Wow that was really good, thanks for the explenation
very useful thanks..
Just what I was looking for! Thank you..
thank you…just what i wanted
Answer 51900c5ee2abd505c200200f
Part 2:
The following is what will be seen in the console:
"I printed"
"Lets see what the values of f1 and f2 are"
None
"I returned"
When function_that_prints
ran, it automatically printed to the console "I printed"
. However, the value stored in f1
is None
because that function had no return
statement.
When function_that_returns
ran, it did not print anything to the console. However, it did return a value, and that value was stored in f2
. When we printed f2
at the end of the code, we saw "I returned"
8 comments
Is there any reason that “ I printed “ comes before “ Let’s see…. “ ?
So what you’re saying is that in: def function_that_prints(): print “I printed”
def function_that_returns(): return “I returned”
f1 = function_that_prints() f2 = function_that_returns() print “Now let us see what the values of f1 and f2 are” print f1 print f2
We could have written anything after function_that_prints() as the print function is just for my eyes and not for the computer’s use? While if using the print function in conjunction with an active (return-ed) def, then it becomes code active, and it works as visual AND code?
Thankyou
thanks
U suck at LIFE
wow?!
Holy cow. It took me about an hour to understand this post but it’s well worth spending the time on it
nuggets
Answer 51900c7d0e194c376300419a
Part 3/3:
Now let us try another example:
def print_a_number(num):
print num
def return_a_number(num):
return num
def add_three(num):
return num + 3
f1 = print_a_number(7)
f2 = return_a_number(2)
print f1
print f2
f3 = add_three(f2)
print f3
f4 = add_three(f1)
print f4
In this example, we pass the results of a function to another function. Let us see what the console will say -
7
None
2
5
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Notice that we got an error when we tried to pass f1
to add_three
because f1
had a value of None
, even though it had printed out the correct number. This is why it is important to return
values and not print
them (except when we want to know what the value is).
I hope that this was a good explanation. If you would like more, let me know and I’ll be happy to try to clarify the best I can.
30 comments
So would it be right to say that return essentially tells us the output of the functions whilst print simply “prints” the function to ,say, console?
It would be better to say that return tells the computer the output, but yes.
Thank you for your help, Michael
In the last part of the output, it should be 2, instead of 4, right?
Ya… Weird… Oops…
Brilliant! Thank you!!
Excellent, thanks so much!
Wow. Bravo.
took time to under stand but thank you
Perfection!
Even I could understand. Big thanks!
Great explanation!!!
i dont get it
v. nice explanation. Thanks!
Example was great!
radjh
cheers m8
Thank you
WHAT THE H*** IS THE ANSWER?!?
idk figure it out yourself
what dose idk mean?
- , dmdamphie don’t you think i’ve already tried that?
@Jaydon Doty, “idk” means “I do not know”, or “I don’t know”.
thanks
no thanks for the attitude jaydon doty
sorry
:{
Thank you so much!!!! It was very very useful!! :D
Shouldn’t ‘print f1’ statement return ‘None’ instead of ‘7’ ?? Because the value stored in f1 would be ‘None’
It does. When it returns the ‘7,’ it is the function running on the line ‘f1 = print_a_number(7)’
Answer 51c055ff9c4e9df20000b476
print displays the results or information to console where as return is used to return a value e.g., u have a function to add two numbers and u wanted to use sum of these two numbers in some other part of code so here we collect the sum which is being returned by sum function.
1 comments
what was the point
Answer 5602a8649113cb3da90005fd
Answer 54918b6451b887663c00204e
print
displays the content to the console, return
returns the value to a variable so you can use the value it just returned later in the program.
in other words. print
is just to print and return
is to return a value.
Answer 54d492d586f55251be0016db
Answer 551d5a629113cb74ac0021b5
Print is what you see on the console, but the return function is what the computer get. Sometimes, we want to see the value of an integer then we print it. And the value of the integer may be returned by some function
Answer 558af26e76b8fe16dd000184
@Suraj:
I had the same question so I tried the following…
def print_a_number(num): print num
f1 = print_a_number(7) print f1
The output was…
7 None None
Answer 55b3a1c09113cb7c38000392
return
is to send the info back to the function to be stored until chosen for the function to be printed
. If it isn’t chosen to be printed, the return
will save the operation to the function.
Answer 55d9cd98e39efee87e000251
print will just print/output any given variable or string onto the screen. return stores the value and this value can be used by the program whenever required. It will not print the value onto the screen.
So basically : If you want to show something to the user - use print If you want to use the value which is computed at the end of a function later in the program - use return
1 comments
Simply put. Thanks!
Answer 55f5da689376760f82000544
Answer 561695c2937676677200009b
Answer 5371a62c7c82cadf35000583
print basically does what it says. It prints in the console. return will not print, rather it can be used to assign value. Return gives us a value, or any data that needs to be assigned to a variable or use print statement to print the returned value
1 comments
boomerasng man
Answer 538ee034282ae366cc005ca6
1 comments
mehmetymeh
Answer 553f105986f552e50c0004df
Answer 5558c95c76b8fea56600019b
Answer 551841c676b8fe184a003e21
1 comments
sjgdflkvmndfb because this makes sense
Answer 553f108be39efea3af000508
I r8 this 8/8 m8 i cant aprci8 this thought ok m8? thax nice b8 m8
9 comments
lol
i am a bit l8
lol
WHAT THE H*** DOES THIS I r8 this 8/8 m8 i cant aprci8 this thought ok m8? thax nice b8 m8 MEAN?
jaydon u suck man u don’t nothing
right, i dont do any thing even though i finished 37% of python and finished make a website
what does “u don’t nothing” mean Maryam Hamad S A Al-Jabri?
he wasnt ready
and he’s saying “ I rate this 8 out of 8 (but its usually 10 out of 10) mate i cant appreciate this thought OK mate? thanks bate mate” this guy has some weird issues with typing and common speech
Answer 54db5285e39efec345001aae
print ‘Welcome to the Pig Latin Translator!’
#Start coding here! original = raw_input(“Enter a word:”) original = “” if len(original) > 0 and original .isalpha(): print original else: print “empty”
Answer 5433d209282ae3b7f4001c20
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons