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
statement?
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
statement?
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:
See the Code Style section of the Python Guide for more advice on this way of using
return
.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
is part of a function definition, whileprint
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 withdef
.Example:
Best thing about
return
function is you can return a value from function but you can do same withprint
so whats the difference ? Basicallyreturn
not about just returning it gives output in object form so that we can save that return value from function to any variable but we can't do withprint
because its same likestdout/cout
inC Programming
.Follow below code for better understanding
CODE
We are now doing our own math functions for
add, subtract, multiply,
anddivide
. The important thing to notice is the last line where we say returna + b
(inadd
). What this does is the following:a
andb
.a + b
. You might say this as, "I adda
andb
then return them."a + b
result to a variable.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.The flow of execution immediately returns to the caller.
In line number 9 :
After the conditional statement gets executed the function ret() gets terminated due to return (line 9). Thus the print("return statement") does not get executed .
This code that appears after return statement or the place the flow of control has not reached is the dead code.
Returning Values
In line number 4 and 8 the return statement is being used to return the value of temporary variable after the condition have been executed.
To bring out the difference between print and return :
Think of the print statement as causing a side-effect, it makes your function write some text out to the user, but it can't be used by another function.
I'll attempt to explain this better with some examples, and a couple definitions from Wikipedia.
Here is the definition of a function from Wikipedia
A function, in mathematics, associates one quantity, the argument of the function, also known as the input, with another quantity, the value of the function, also known as the output..
Think about that for a second. What does it mean when you say the function has a value?
What it means is that you can actually substitute the value of a function with a normal value! (Assuming the two values are the same type of value)
Why would you want that you ask?
What about other functions that may accept the same type of value as an input?
There is a fancy mathematical term for functions that only depend on their inputs to produce their outputs: Referential Transparency. Again, a definition from Wikipedia.
Referential transparency and referential opaqueness are properties of parts of computer programs. An expression is said to be referentially transparent if it can be replaced with its value without changing the behavior of a program
It might be a bit hard to grasp what this means if you're just new to programming, but I think you will get it after some experimentation. In general though, you can do things like print in a function, and you can also have a return statement at the end.
Just remember that when you use return you are basically saying "A call to this function is the same as writing the value that gets returned"
Python will actually insert a return value for you if you decline to put in your own, it's called "None", and it's a special type that simply means nothing, or null.