What's the difference between return and print



This is the correct answer, but i just want to ask:
Do def functions have to have 'return' instead of 'print'?

def answer():
    return abs(-42)


A lot. They are in no way related.

print is immediate output to the display. If it is the evaluation of an expression, such as,

print abs(-42)

there is no trace of the value once displayed, except on the screen.

return abs(x)

on the other hand sends the result back to the caller.

mol = answer(-42)

print mol

The caller can assign the return value to a variable in that scope, and retrieve it for output or other process at any time during a session.


Could you elaborate a bit?


Just did. See above. What understanding have you thus far concerning scope? It relates to this question.


I don't really know about scope. But am i correct in saying print only gives you the result at the moment while return allows you to see the value for longer?


Yes, for the most part. So long as you understand why we can see it for longer.

print does not preserve the result. return lets the function send it back to the caller, which is where it can be preserved for future other uses.

As for scope, think of it as a building that you can only see out of, and not in. That is what function scope is like. It can see out to other variables and functions, but they cannot see in, so locally defined variables are protected in their own environment.

When we define a parameter of a function,

def answer(x):

that parameter is a local variable. Any change made to it inside the function will have no effect outside. In order for the outside to be able to see the changed or computed value, it must be sent back out, or returned.

caller => (argument) => callee (parameter) => eval => return value => caller => assignment


Part of understanding scope involves recognizing the data types being exchanged. When we call a function with a value or expression as argument, the parameter is a copy of the value, unique and distinct. That is why we cannot see what happens to it inside the function. It must be returned or it is lost when the function terminates.

Functions do not have to have return statements. They terminate and hand flow back to the caller, where execution resumes. Often you will see a function with return True or return 0 at the end and wonder what it means.

For starters, it may just be the author telling a reader that there is no return value, and they are writing a return line because it is in their style guide. Not always, though. It may be of particular meaning, such is a number even? Or is a number prime? Nonetheless, it's too early to consider style, so long as we know return is only needed when we wish to send data back to the caller.

Data does not only have type, but also structure. There are two ways to exchange data with a function. By value or expression, (expressions yield a value), or by reference to a data structure such as a list, tuple, set, dictionary, or by reference to a function, which output (return) will be passed to function as the argument.

Reference objects do not take on local scope. A list that is manipulated inside a function will be changed, even if there is no return value.

nums = [1,2,3,4,5]
def squares():
    for i in range(len(nums)):
        nums[i] **= 2
print nums

# [1, 4, 9, 16, 25]

Because the nums object can be seen by the function, we did not have to pass it in. Mind this function is dedicated to nums and cannot operate on any other object. A subject that will come up later.


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