This question already has an answer here:
Lets say I define a simple function which will display an integer passed to it:
def funct1(param1):
print(param1)
return(param1)
the output will be the same but and I know that when a return
statement is used in a function the output can be used again. Otherwise the value of a print
statement cannot be used. But I know this is not the formal definition, Can anyone provide me with a good definition?
print
(orprint()
if you're using Python 3) does exactly that—print anything that follows the keyword. It will also do nice things like automatically join multiple values with a space:Otherwise
print
(print()
) will do nothing from your program's point of view. It will not affect the control flow in any way and execution will resume with the very next instruction in your code block:On the other hand
return
(notreturn()
) is designed to immediately break the control flow and exit the current function and return a specified value to the caller that called your function. It will always do this and just this.return
itself will not cause anything to get printed to the screen. Even if you don't specify a return value an implicitNone
will get returned. If you skip areturn
altogether, an implicitreturn None
will still happen at the end of your function:The reason you see
return
ed values printed to the screen is because you are probably working in the interactive Python shell that automaticallyprint
s any result for your own convenience.Dramatically different things. Imagine if I have this python program:
What do you expect to be the output?
Why?
Why? Because
print
takes its arguments/expressions and dumps them to standard output, so in the functions I made up,print
will output the value ofx
, which ishello
.printAndReturn
will returnx
to the caller of the method, so:ret = printAndReturn()
ret
will have the same value asx
, i.e."hello"
printAndReturnNothing
doesn't return anything, so:other = printAndReturnNothing()
other
actually becomesNone
because that is the default return from a python function. Python functions always return something, but if noreturn
is declared, the function will returnNone
.Resources
Going through the python tutorial will introduce you to these concepts: http://docs.python.org/tutorial
Here's something about functions form python's tutorial: http://docs.python.org/tutorial/controlflow.html#defining-functions
Simple example to show the difference:
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:
The output is only the same in the interactive terminal. When you execute your program normally the results will be completely different.
I would suggest you to read a book about Python or read a tutorial that teaches you the basics, because this is a very basic thing.
With
print()
you will display to standard output the value ofparam1
, while withreturn
you will sendparam1
to the caller.The two statements have a very different meaning, and you should not see the same behaviour. Post your whole program and it'll be easier to point out the difference to you.
Edit: as pointed by others, if you are in an interactive python shell you see the same effect (the value is printed), but that happens because the shell evaluates expressions and prints their output.
In this case, a function with a
return
statement is evaluated as the parameter ofreturn
itself, so the return value is echoed back. Don't let the interactive shell fool you! :)