Profile image of anonymous
Submitted by anonymous
about 12 years

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

And what does the return function actually mean, please?

Answer 51900c3fa31802c99c0043c6

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
Profile image of anonymous
Submitted by anonymous
about 12 years

18 comments

Profile image of anonymous
Submitted by anonymous
over 11 years

“No one” Hush your gums. It’s not your fault that you’re unable to comprehend his explanation.

Profile image of anonymous
Submitted by anonymous
over 11 years

ikr

Profile image of anonymous
Submitted by anonymous
about 11 years

looks great :)

Profile image of anonymous
Submitted by anonymous
about 11 years

very helpful

Profile image of anonymous
Submitted by anonymous
almost 11 years

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

Profile image of anonymous
Submitted by anonymous
almost 11 years

This most certainly helps. Thank you!

Profile image of anonymous
Submitted by anonymous
over 10 years

this sux I didn’t understand

Profile image of anonymous
Submitted by anonymous
about 10 years

Hello

Profile image of anonymous
Submitted by anonymous
about 10 years

Hi BaronGinger

Profile image of anonymous
Submitted by anonymous
about 10 years

hello jack, hows’ goin????

Profile image of anonymous
Submitted by anonymous
about 10 years

I’m fine thanks hows u

Profile image of anonymous
Submitted by anonymous
about 10 years

top notch spllin

Profile image of anonymous
Submitted by anonymous
about 10 years

top qual

Profile image of anonymous
Submitted by anonymous
about 10 years

thank you

Profile image of anonymous
Submitted by anonymous
about 10 years

Wow that was really good, thanks for the explenation

Profile image of anonymous
Submitted by anonymous
almost 10 years

very useful thanks..

Profile image of anonymous
Submitted by anonymous
almost 10 years

Just what I was looking for! Thank you..

Profile image of anonymous
Submitted by anonymous
almost 10 years

thank you…just what i wanted

Answer 51900c5ee2abd505c200200f

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"

Profile image of anonymous
Submitted by anonymous
about 12 years

8 comments

Profile image of anonymous
Submitted by anonymous
over 10 years

Is there any reason that “ I printed “ comes before “ Let’s see…. “ ?

Profile image of anonymous
Submitted by anonymous
over 10 years

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?

Profile image of anonymous
Submitted by anonymous
over 10 years

Thankyou

Profile image of anonymous
Submitted by anonymous
about 10 years

thanks

Profile image of anonymous
Submitted by anonymous
about 10 years

U suck at LIFE

Profile image of anonymous
Submitted by anonymous
about 10 years

wow?!

Profile image of anonymous
Submitted by anonymous
almost 10 years

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

Profile image of anonymous
Submitted by anonymous
over 9 years

nuggets

Answer 51900c7d0e194c376300419a

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.

Profile image of anonymous
Submitted by anonymous
about 12 years

30 comments

Profile image of anonymous
Submitted by anonymous
almost 12 years

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?

Profile image of anonymous
Submitted by anonymous
almost 12 years

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

Profile image of anonymous
Submitted by anonymous
almost 12 years

Thank you for your help, Michael

Profile image of anonymous
Submitted by anonymous
almost 12 years

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

Profile image of anonymous
Submitted by anonymous
almost 12 years

Ya… Weird… Oops…

Profile image of anonymous
Submitted by anonymous
over 11 years

Brilliant! Thank you!!

Profile image of anonymous
Submitted by anonymous
over 11 years

Excellent, thanks so much!

Profile image of anonymous
Submitted by anonymous
about 11 years

Wow. Bravo.

Profile image of anonymous
Submitted by anonymous
about 11 years

took time to under stand but thank you

Profile image of anonymous
Submitted by anonymous
almost 11 years

Perfection!

Profile image of anonymous
Submitted by anonymous
almost 11 years

Even I could understand. Big thanks!

Profile image of anonymous
Submitted by anonymous
almost 11 years

Great explanation!!!

Profile image of anonymous
Submitted by anonymous
over 10 years

i dont get it

Profile image of anonymous
Submitted by anonymous
over 10 years

v. nice explanation. Thanks!

Profile image of anonymous
Submitted by anonymous
over 10 years

Example was great!

Profile image of anonymous
Submitted by anonymous
about 10 years

radjh

Profile image of anonymous
Submitted by anonymous
about 10 years

cheers m8

Profile image of anonymous
Submitted by anonymous
about 10 years

Thank you

Profile image of anonymous
Submitted by anonymous
about 10 years

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

Profile image of anonymous
Submitted by anonymous
about 10 years

idk figure it out yourself

Profile image of anonymous
Submitted by anonymous
about 10 years

what dose idk mean?

Profile image of anonymous
Submitted by anonymous
about 10 years
  • , dmdamphie don’t you think i’ve already tried that?
Profile image of anonymous
Submitted by anonymous
about 10 years

@Jaydon Doty, “idk” means “I do not know”, or “I don’t know”.

Profile image of anonymous
Submitted by anonymous
about 10 years

thanks

Profile image of anonymous
Submitted by anonymous
about 10 years

no thanks for the attitude jaydon doty

Profile image of anonymous
Submitted by anonymous
about 10 years

sorry

Profile image of anonymous
Submitted by anonymous
about 10 years

:{

Profile image of anonymous
Submitted by anonymous
almost 10 years

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

Profile image of anonymous
Submitted by anonymous
almost 10 years

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

Profile image of anonymous
Submitted by anonymous
almost 10 years

It does. When it returns the ‘7,’ it is the function running on the line ‘f1 = print_a_number(7)’

Answer 51c055ff9c4e9df20000b476

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.

Profile image of anonymous
Submitted by anonymous
almost 12 years

1 comments

Profile image of anonymous
Submitted by anonymous
over 10 years

what was the point

Answer 5602a8649113cb3da90005fd

2 votes

Permalink

brav

Profile image of anonymous
Submitted by anonymous
over 9 years

Answer 54918b6451b887663c00204e

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.

Profile image of anonymous
Submitted by anonymous
over 10 years

Answer 54d492d586f55251be0016db

1 vote

Permalink

I don’t get it

Profile image of anonymous
Submitted by anonymous
over 10 years

Answer 551d5a629113cb74ac0021b5

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

Profile image of anonymous
Submitted by anonymous
about 10 years

Answer 558af26e76b8fe16dd000184

1 vote

Permalink

@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

Profile image of anonymous
Submitted by anonymous
almost 10 years

Answer 55b3a1c09113cb7c38000392

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.

Profile image of anonymous
Submitted by anonymous
almost 10 years

Answer 55d9cd98e39efee87e000251

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

Profile image of anonymous
Submitted by anonymous
almost 10 years

1 comments

Profile image of anonymous
Submitted by anonymous
over 9 years

Simply put. Thanks!

Answer 55f5da689376760f82000544

1 vote

Permalink

print => console return => function

Profile image of anonymous
Submitted by anonymous
over 9 years

Answer 561695c2937676677200009b

0 votes

Permalink

there correct

Profile image of anonymous
Submitted by anonymous
over 9 years

Answer 5371a62c7c82cadf35000583

-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

Profile image of anonymous
Submitted by anonymous
about 11 years

1 comments

Profile image of anonymous
Submitted by anonymous
about 10 years

boomerasng man

Answer 538ee034282ae366cc005ca6

-2 votes

Permalink

thanks for your help

Profile image of anonymous
Submitted by anonymous
almost 11 years

1 comments

Profile image of anonymous
Submitted by anonymous
about 10 years

mehmetymeh

Answer 553f105986f552e50c0004df

-2 votes

Permalink

it is kinda hard tho

Profile image of anonymous
Submitted by anonymous
about 10 years

Answer 5558c95c76b8fea56600019b

-2 votes

Permalink

i no get it

Profile image of anonymous
Submitted by anonymous
about 10 years

Answer 551841c676b8fe184a003e21

-3 votes

Permalink

sjgdflkvmndfb

Profile image of anonymous
Submitted by anonymous
about 10 years

1 comments

Profile image of anonymous
Submitted by anonymous
about 10 years

sjgdflkvmndfb because this makes sense

Answer 553f108be39efea3af000508

-3 votes

Permalink

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

Profile image of anonymous
Submitted by anonymous
about 10 years

9 comments

Profile image of anonymous
Submitted by anonymous
about 10 years

lol

Profile image of anonymous
Submitted by anonymous
about 10 years

i am a bit l8

Profile image of anonymous
Submitted by anonymous
about 10 years

lol

Profile image of anonymous
Submitted by anonymous
about 10 years

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

Profile image of anonymous
Submitted by anonymous
about 10 years

jaydon u suck man u don’t nothing

Profile image of anonymous
Submitted by anonymous
almost 10 years

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

Profile image of anonymous
Submitted by anonymous
almost 10 years

what does “u don’t nothing” mean Maryam Hamad S A Al-Jabri?

Profile image of anonymous
Submitted by anonymous
over 9 years

he wasnt ready

Profile image of anonymous
Submitted by anonymous
over 9 years

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

-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”

Profile image of anonymous
Submitted by anonymous
over 10 years

Answer 5433d209282ae3b7f4001c20

-13 votes

Permalink

Thank you :)

Profile image of anonymous
Submitted by anonymous
over 10 years