This forum is now read-only. Please use our new forums at discuss.codecademy.com.

302 points
Picture
Submitted by
Janine Kagle
over 6 years ago

What's the difference between the print and the return functions?

And what does the return function actually mean, please?


189 votes

Best answer

permalink

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

1703 points
4f84976a93d918000300de59_981251210
Submitted by
Michael Rochlin
over 6 years ago

18 Comments

5282c46d548c355a51000a9f_662968330 Shane Lee O'Shea almost 6 years ago

"No one" Hush your gums. It's not your fault that you're unable to comprehend his explanation.

53065a0b631fe945c7000225_373732363 Anthony Smith almost 6 years ago

ikr

5f00cbb2e3a2b5e6215885b147e21e80?s=140&d=retro Hristiyan Lazarov over 5 years ago

looks great :)

5369ba7b8c1ccc2d7b000743_675661424 cecil y. h. cheng over 5 years ago

very helpful

526be5fef10c60b3650005d0_378807088 Noureddne Rached over 5 years ago

Thanks Man You really helped me get it in the first couple lines

53f6e1bb9c4e9dac3d000574_54247016 CeCe over 5 years ago

This most certainly helps. Thank you!

9fa24f8f2890e733e149bb8b5f7dfd5b?s=140&d=retro Aaronchannell almost 5 years ago

this sux I didn't understand

550aa90a9376769a4c002dba_855051194 LukeHasABuiltInBikeHelmet over 4 years ago

Hello

550aa997d3292f5242002baa_989659371 IAmNotGinger over 4 years ago

Hi BaronGinger

550aa90a9376769a4c002dba_855051194 LukeHasABuiltInBikeHelmet over 4 years ago

hello jack, hows' goin????

550aa997d3292f5242002baa_989659371 IAmNotGinger over 4 years ago

I'm fine thanks hows u

550aa90a9376769a4c002dba_855051194 LukeHasABuiltInBikeHelmet over 4 years ago

top notch spllin

550aa90a9376769a4c002dba_855051194 LukeHasABuiltInBikeHelmet over 4 years ago

top qual

D92e2979f77ed9db3a554956a73ec1be?s=140&d=retro Cristian9804 over 4 years ago

thank you

2ea5c7d4d2ede1c8853d8d6cb5c23fa9?s=140&d=retro themodman101 over 4 years ago

Wow that was really good, thanks for the explenation

Picture Rix Vii over 4 years ago

very useful thanks..

55cac6bd93767691ce0002f7_384917797 ChrisNight_558aa7e9e39efe60ce00052f_ over 4 years ago

Just what I was looking for! Thank you..

28b8ef37d941655bbe2b0a790d7bb2de?s=140&d=retro rohanaj99 over 4 years ago

thank you...just what i wanted


91 votes

permalink

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"

1703 points
4f84976a93d918000300de59_981251210
Submitted by
Michael Rochlin
over 6 years ago

8 Comments

4d1f3fe2a3535a6b58ad9e6ec3ae0956?s=140&d=retro Tsungho about 5 years ago

Is there any reason that " I printed " comes before " Let's see.... " ?

2ce7a3e6efe3aea82238ccd0f79c449e?s=140&d=retro G13Strong about 5 years ago

So what you're saying is that in:
def functionthatprints():
print "I printed"

def functionthatreturns():
return "I returned"

f1 = functionthatprints()
f2 = functionthatreturns()
print "Now let us see what the values of f1 and f2 are"
print f1
print f2

We could have written anything after functionthatprints() 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?

4b437da429e16debecbbbd02e455cb63?s=140&d=retro give1take2 almost 5 years ago

Thankyou

550aa90a9376769a4c002dba_855051194 LukeHasABuiltInBikeHelmet over 4 years ago

thanks

55396496e39efefac300018c_286646016 Isaac Mendoza over 4 years ago

U suck at LIFE

5552074751b887a27200003a_977072772 dmdamphie over 4 years ago

wow?!

Bcdd687ced4032d534ee58de78e94349?s=140&d=retro PaulFish over 4 years ago

Holy cow. It took me about an hour to understand this post but it's well worth spending the time on it

E91e8a4247717e37053a68b028f2883c?s=140&d=retro shrek_nugget123 about 4 years ago

nuggets


80 votes

permalink

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.

1703 points
4f84976a93d918000300de59_981251210
Submitted by
Michael Rochlin
over 6 years ago

30 Comments

0afb4a5dde595f6df946dfb4cb5e4c0e?s=140&d=retro Kokalz over 6 years ago

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?

4f84976a93d918000300de59_981251210 Michael Rochlin over 6 years ago

It would be better to say that return tells the computer the output, but yes.

0afb4a5dde595f6df946dfb4cb5e4c0e?s=140&d=retro Kokalz over 6 years ago

Thank you for your help, Michael

D815e0c67335177d6d1ebcf9b22cdf11?s=140&d=retro ZhongLi over 6 years ago

In the last part of the output, it should be 2, instead of 4, right?

4f84976a93d918000300de59_981251210 Michael Rochlin over 6 years ago

Ya... Weird... Oops...

Picture Ryan Johnson about 6 years ago

Brilliant! Thank you!!

86925032c50836b510feea46e5f33f3f?s=140&d=retro Jordan Block almost 6 years ago

Excellent, thanks so much!

Dcb721e43d67edabd388396d1ca013b4?s=140&d=retro BiddyBot almost 6 years ago

Wow. Bravo.

53342b9a548c35b5c70026e2_742616776 rich robbert over 5 years ago

took time to under stand but thank you

508274f3e3c68c65efed31e2fdd75aad?s=140&d=retro Joshua Jankus over 5 years ago

Perfection!

Df7bca863d814770aee8117d2a871998?s=140&d=retro drikidan over 5 years ago

Even I could understand. Big thanks!

F37eb166b8cbc45c3f27bf709ff84cb6?s=140&d=retro Casey Wendel over 5 years ago

Great explanation!!!

547ef7b78c1ccc5c730019de_185010760 Jake Sanders about 5 years ago

i dont get it

7f399d433f3aaf6b584cec9b263e5352?s=140&d=retro Jacob Heil about 5 years ago

v. nice explanation. Thanks!

Picture Tan Zhi You almost 5 years ago

Example was great!

54b7b5db95e378639e0020f6_954969218 Steven Chalmers almost 5 years ago

radjh

550aa90a9376769a4c002dba_855051194 LukeHasABuiltInBikeHelmet over 4 years ago

cheers m8

03144446a4b15358aecf3433198b1dd5?s=140&d=retro sashraf11 over 4 years ago

Thank you

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

WHAT THE H*** IS THE ANSWER?!?

5552074751b887a27200003a_977072772 dmdamphie over 4 years ago

idk figure it out yourself

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

what dose idk mean?

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

  • , dmdamphie don't you think i've already tried that?

5550bff2e39efe0edc00080e_966766145 TheWatchTower over 4 years ago

@Jaydon Doty, "idk" means "I do not know", or "I don't know".

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

thanks

5552074751b887a27200003a_977072772 dmdamphie over 4 years ago

no thanks for the attitude jaydon doty

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

sorry

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

:{

59ee0e249e8471de6b0000d0_502401554 Tania Fonseca over 4 years ago

Thank you so much!!!! It was very very useful!! :D

68c2ba2d6f4b6c9e509c31c653ce555e?s=140&d=retro Suraj Tata Prasad over 4 years ago

Shouldn't 'print f1' statement return 'None' instead of '7' ?? Because the value stored in f1 would be 'None'

Picture rzarakta over 4 years ago

It does. When it returns the '7,' it is the function running on the line 'f1 = printanumber(7)'


5 votes

permalink

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.

623 points
Picture
Submitted by
Varun Sriram
over 6 years ago

1 Comment

A3a0ca91e81a9342abb5c5f6ef843f5a?s=140&d=retro Nabil CHOWDHURY about 5 years ago

what was the point


2 votes

permalink

brav

93 points
561e5a079113cb16c1000671_754570536
Submitted by
Sam Stokes
about 4 years ago


1 vote

permalink

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.

318 points
Ffc3f501d515ba67c7906cd4b45bf609?s=140&d=retro
Submitted by
biwin
almost 5 years ago


1 vote

permalink

I don't get it

149 points
54d4916c937676ea9700182c_971315369
Submitted by
corben_brooks
almost 5 years ago


1 vote

permalink

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

321 points
554c4f0d93767674a00007b0_525711245
Submitted by
UchihaWind
over 4 years ago


1 vote

permalink

@Suraj:

I had the same question so I tried the following...

def printanumber(num):
print num

f1 = printanumber(7)
print f1

The output was...

7
None
None

1431 points
4d47c496e94ad8fa8d7a32c3f5c6f9ad?s=140&d=retro
Submitted by
Ari Ogoke
over 4 years ago


1 vote

permalink

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.

203 points
55b3a21676b8fe6543000285_640139606
Submitted by
Slass33
over 4 years ago


1 vote

permalink

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

465 points
366eaf0a3cd4855fc99ee017f586dbc8?s=140&d=retro
Submitted by
Ryan Rodrigues
over 4 years ago

1 Comment

466fa5dacfa2ab978f4960f1d732c8c0?s=140&d=retro meghasuresh91 about 4 years ago

Simply put. Thanks!


1 vote

permalink

print => console
return => function

537 points
13ee58ae68f8613e447cd4a0eaa1b9de?s=140&d=retro
Submitted by
Kamalaksha
about 4 years ago


0 votes

permalink

there correct

233 points
Eb0053dea35b937769ff1a1c9b144fad?s=140&d=retro
Submitted by
colly1
about 4 years ago


-2 votes

permalink

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

610 points
Picture
Submitted by
Karun Shakya
over 5 years ago

1 Comment

550aa90a9376769a4c002dba_855051194 LukeHasABuiltInBikeHelmet over 4 years ago

boomerasng man


-2 votes

permalink

thanks for your help

58 points
76b85cd0d3a36394fa5f56cfdc93bc8a?s=140&d=retro
Submitted by
HamzaRashid
over 5 years ago

1 Comment

550aa90a9376769a4c002dba_855051194 LukeHasABuiltInBikeHelmet over 4 years ago

mehmetymeh


-2 votes

permalink

it is kinda hard tho

90 points
547d3ad58c1ccca7d7001179_919322695
Submitted by
khiri37
over 4 years ago


-2 votes

permalink

i no get it

58 points
A4ce3f4ffc809966e0ae72f74f6c7771?s=140&d=retro
Submitted by
johnjohn5202
over 4 years ago


-3 votes

permalink

sjgdflkvmndfb

63 points
54d27020d3292fe8a70008cc_805516323
Submitted by
Rayhan Islam
over 4 years ago

1 Comment

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

sjgdflkvmndfb because this makes sense


-3 votes

permalink

I r8 this 8/8 m8 i cant aprci8 this thought ok m8? thax nice b8 m8

90 points
547d3ad58c1ccca7d7001179_919322695
Submitted by
khiri37
over 4 years ago

9 Comments

547d3ad58c1ccca7d7001179_919322695 khiri37 over 4 years ago

lol

547d3ad58c1ccca7d7001179_919322695 khiri37 over 4 years ago

i am a bit l8

547d3ad58c1ccca7d7001179_919322695 khiri37 over 4 years ago

lol

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

WHAT THE H*** DOES THIS I r8 this 8/8 m8 i cant aprci8 this thought ok m8? thax nice b8 m8 MEAN?

54ac09a476b8fe19120019c0_196142210 Maryam Hamad S A Al-Jabri over 4 years ago

jaydon u suck man u don't nothing

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

right, i dont do any thing even though i finished 37% of python and finished make a website

63092a89bcdf0e8899749db0a145c60f?s=140&d=retro Jaydon Doty over 4 years ago

what does "u don't nothing" mean Maryam Hamad S A Al-Jabri?

26f3586d62963507a753719ce1e0a266?s=140&d=retro 2illusions over 4 years ago

he wasnt ready

26f3586d62963507a753719ce1e0a266?s=140&d=retro 2illusions over 4 years ago

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


-5 votes

permalink

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"

95 points
Dc6cc262abf4bf42242029dd02e091c9?s=140&d=retro
Submitted by
Luke Ashfield
almost 5 years ago


-13 votes

permalink

Thank you :)

67 points
541ad6647c82ca3998002606_900467198
Submitted by
Josh Phillips
about 5 years ago