What is the simple basic explanation of what the return statement is, how to use it in Python?
And what is the difference between it and the print
state
The print()
function writes, i.e., "prints", a string in the console. The return
statement causes your function to exit and hand back a value to its caller. The point of functions in general is to take in inputs and return something. The return
statement is used when a function is ready to return a value to its caller.
For example, here's a function utilizing both print()
and return
:
def foo():
print("hello from inside of foo")
return 1
Now you can run code that calls foo, like so:
if __name__ == '__main__':
print("going to call foo")
x = foo()
print("called foo")
print("foo returned " + str(x))
If you run this as a script (e.g. a .py
file) as opposed to in the Python interpreter, you will get the following output:
going to call foo
hello from inside foo
called foo
foo returned 1
I hope this makes it clearer. The interpreter writes return values to the console so I can see why somebody could be confused.
Here's another example from the interpreter that demonstrates that:
>>> def foo():
... print("hello from within foo")
... return 1
...
>>> foo()
hello from within foo
1
>>> def bar():
... return 10 * foo()
...
>>> bar()
hello from within foo
10
You can see that when foo()
is called from bar()
, 1 isn't written to the console. Instead it is used to calculate the value returned from bar()
.
print()
is a function that causes a side effect (it writes a string in the console), but execution resumes with the next statement. return
causes the function to stop executing and hand a value back to whatever called it.
I think the dictionary is your best reference here
Return and Print
In short:
return gives something back or replies to the caller of the function while print produces text
return
should be used for recursive functions/methods or you want to use the returned value for later applications in your algorithm.
print
should be used when you want to display a meaningful and desired output to the user and you don't want to clutter the screen with intermediate results that the user is not interested in, although they are helpful for debugging your code.
The following code shows how to use return
and print
properly:
def fact(x):
if x < 2:
return 1
return x * fact(x - 1)
print(fact(5))
This explanation is true for all of the programming languages not just python.
This answer goes over some of the cases that have not been discussed above.
The return statement allows you to terminate the execution of a function before you reach the end. This causes the flow of execution to immediately return to the caller.
In line number 4:
def ret(n):
if n > 9:
temp = "two digits"
return temp #Line 4
else:
temp = "one digit"
return temp #Line 8
print("return statement")
ret(10)
After the conditional statement gets executed the ret()
function gets terminated due to return temp
(line 4).
Thus the print("return statement")
does not get executed.
Output:
two digits
This code that appears after the conditional statements, or the place the flow of control cannot reach, is the dead code.
Returning Values
In lines number 4 and 8, the return statement is being used to return the value of a temporary variable after the condition has been executed.
To bring out the difference between print and return:
def ret(n):
if n > 9:
print("two digits")
return "two digits"
else :
print("one digit")
return "one digit"
ret(25)
Output:
two digits
'two digits'
return
is part of a function definition, while print
outputs text to the standard output (usually the console).
A function is a procedure accepting parameters and returning a value. return
is for the latter, while the former is done with def
.
Example:
def timestwo(x):
return x*2
Just to add to @Nathan Hughes's excellent answer:
The return
statement can be used as a kind of control flow. By putting one (or more) return
statements in the middle of a function, we can say: "stop executing this function. We've either got what we wanted or something's gone wrong!"
Here's an example:
>>> def make_3_characters_long(some_string):
... if len(some_string) == 3:
... return False
... if str(some_string) != some_string:
... return "Not a string!"
... if len(some_string) < 3:
... return ''.join(some_string,'x')[:,3]
... return some_string[:,3]
...
>>> threechars = make_3_characters_long('xyz')
>>> if threechars:
... print threechars
... else:
... print "threechars is already 3 characters long!"
...
threechars is already 3 characters long!
See the Code Style section of the Python Guide for more advice on this way of using return
.