Difference between print and return, and when to use it?

lesson
https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/strings-in-functions?action=resume_content_item

CORRECT:
n = “Hello”

Your function here!

def string_function(s):
word = s + “world”
return word

print string_function(n)

WRONG:
n = “Hello”

Your function here!

def string_function(s):
word = s + “world”
print word

print string_function(n)

Why does print not work for this?

1 Like

print -> value displayed then gone from memory.

return -> value handed back to caller

We can print from anywhere, but can only return from a function. Printing has no data bindings. Returned values are bindable.

3 Likes

Unfortunately, there is a character limit so this will be in many parts. First thing to note is that return and print are statements, not functions, but that is just semantics.

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:

def function_that_prints():
    print "I printed"

def function_that_returns():
    return "I returned"

f1 = function_that_prints()
f2 = function_that_returns()
print "Now let us see what the values of f1 and f2 are"
print f1
print f2
2 Likes

Alright, thank you mtf and rajesh19977 for such prompt replies. I sort of get it, now that you said every function requires a return otherwise a None will be returned.

2 Likes
#!/usr/bin/env python

def printAndReturnNothing():
    x = "hello"
    print(x)

def printAndReturn():
    x = "hello"
    print(x)
    return x

def main():
    ret = printAndReturn()
    other = printAndReturnNothing()

    print("ret is: %s" % ret)
    print("other is: %s" % other)

if __name__ == "__main__":
    main()

What do you expect to be the output?

hello
hello
ret is : hello
other is: None

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 of x , which is hello .

  • printAndReturn will return x to the caller of the method, so: ret = printAndReturn()

ret will have the same value as x , i.e. "hello"

  • printAndReturnNothing doesn’t return anything, so: other = printAndReturnNothing()

other actually becomes None because that is the default return from a python function. Python functions always return something, but if no return is declared, the function will return None .

Hi mtf,

This was a very clear answer. However I was wondering; in a function that has been defined to return a value, is it possible to store that returned value in a variable within that function and use the print command to show the value on the console. For example:

def distance_from_zero(disFrmZero):

if(type(disFrmZero) == int or type(disFrmZero) == float):
return abs(disFrmZero)
ans = abs(disFrmZero)
print ans

else:
return “Nope”
print abs(disFrmZero)
ans = abs(disFrmZero)
print ans

distance_from_zero(7.0)

I tried messing about with other variations to see if I could get my desired outcome but it was not working:

def distance_from_zero(disFrmZero):
if(type(disFrmZero) == int or type(disFrmZero) == float):
return abs(disFrmZero)
ans = abs(disFrmZero)

else:
return “Nope”
print abs(disFrmZero)
ans = abs(disFrmZero)

distance_from_zero(7.0)
print ans

Or have i gone totally off on this one

Thanks

The problem is that once return is reached,*the function halts. Therefore, no statements following return will execute.

You can always print() the return value to the console before the return statement, but I think that as you do more coding, you will do less printing, except during debugging, where print() is immensely helpful.

Except for the simplest cases, if one of the requirements of your script is to print information to the console, you may discover that the way to go is with one output function (or at least a block within a function) to which the necessary information is passed via variables or return statements, and whose sole duty is to nicely format and print the output.

If you just would like to see the returned value of a function, the best way is via the function call:

def hello(name):
    return "Hello, {}!".format(name)

print(hello('world'))

# Output:
Hello, world!

1 Like

Yes, it is, but it must happen before the return since anything after is not reachable.

 a = value
 print (a)
 return a
1 Like

Thanks a lot for the clarification

Thanks for the clarification. Nicely summarized

1 Like