Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the return 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?

like image 674
Hitesh Kumar Avatar asked Aug 20 '11 02:08

Hitesh Kumar


People also ask

What is the purpose of a return statement in Python?

The Python return statement is a key component of functions and methods. You can use the return statement to make your functions send Python objects back to the caller code. These objects are known as the function's return value. You can use them to perform further computation in your programs.

What is the purpose of return statement in function Mcq?

4. What is the purpose of a return statement in a function? Explanation: The return stops the execution of the function when it is encountered within the function. It returns the value to the statement where the function is called.

What is the purpose of return statement in Java?

A return statement causes the program control to transfer back to the caller of a method. Every method in Java is declared with a return type and it is mandatory for all java methods. A return type may be a primitive type like int, float, double, a reference type or void type(returns nothing).


2 Answers

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.

like image 176
Nathan Hughes Avatar answered Oct 10 '22 05:10

Nathan Hughes


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?

def square(n):     return n * n  def add_one(n):     return n + 1  print square(12)  # square(12) is the same as writing 144  print add_one(square(12)) print add_one(144) #These both have the same output 

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.

like image 38
Wes Avatar answered Oct 10 '22 05:10

Wes