Difference between print and return?


#1




Hello! This is my first time posting and I was wondering what the difference between print and return is. If someone could explain this it would be much appreciated :slight_smile:



#2

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

Source of an answer!


#3

Ah thankyou for the reply! Okay that clears it up for me I think. Return is kinda like 'print' but for the computer, not us! (Please correct me if I'm wrong in this loose analogy)
Addendum question: What does def mean? I assume it means define...
I think I should read the Python Glossary tomorrow morning


#4

def is a keyword you need to define a function.

no? return is something for functions. By default None is returned by functions. By defining a return keyword, you can cause the function to return something else, and this you could stored in a variable or printed

return literal hands you something back, as you can beautiful see in @coreblaster45820 answer:

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

please run this code, you will see that print f1:

print f1

prints None, since None is returned by the function (the default value)

where as f2 will print I returned.

print does only one thing: print to the output.


#5

Bypassed me with an answer by a minute))

@groovydoovy hope all that said above helps you!


#6

you could still answer it? multiply angles/points of view won't hurt, in fact, it would be good, in particular if a concept requires explanation.


#7

Ah so when we define a function to return something, we change the "None" output into whatever we say after return.

So defining is like the instructions for the function, which tells it what to do? So whatever is in the indent is what the function does...?

Edit: Looks like I jumped the gun because I'm now learning about functions in unit 7
Thanks for your help @stetim94


#8

no more questions about it?


#9

No I think that solves it for me, thanks again.


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.