[ACCEPTED]-How is returning the output of a function different from printing it?-return

Accepted answer
Score: 72

print simply prints out the structure to your 15 output device (normally the console). Nothing 14 more. To return it from your function, you 13 would do:

def autoparts():
  parts_dict = {}
  list_of_parts = open('list_of_parts.txt', 'r')
  for line in list_of_parts:
        k, v = line.split()
        parts_dict[k] = v
  return parts_dict

Why return? Well if you don't, that 12 dictionary dies (gets garbage collected) and 11 is no longer accessible as soon as this 10 function call ends. If you return the value, you 9 can do other stuff with it. Such as:

my_auto_parts = autoparts() 
print(my_auto_parts['engine']) 

See 8 what happened? autoparts() was called and it returned 7 the parts_dict and we stored it into the my_auto_parts variable. Now 6 we can use this variable to access the dictionary 5 object and it continues to live even though 4 the function call is over. We then printed 3 out the object in the dictionary with the 2 key 'engine'.

For a good tutorial, check out dive into python. It's 1 free and very easy to follow.

Score: 11

The print statement will output an object 6 to the user. A return statement will allow 5 assigning the dictionary to a variable once the function is finished.

>>> def foo():
...     print "Hello, world!"
... 
>>> a = foo()
Hello, world!
>>> a
>>> def foo():
...     return "Hello, world!"
... 
>>> a = foo()
>>> a
'Hello, world!'

Or 4 in the context of returning a dictionary:

>>> def foo():
...     print {'a' : 1, 'b' : 2}
... 
>>> a = foo()
{'a': 1, 'b': 2}
>>> a
>>> def foo():
...     return {'a' : 1, 'b' : 2}
... 
>>> a = foo()
>>> a
{'a': 1, 'b': 2}

(The 3 statements where nothing is printed out 2 after a line is executed means the last 1 statement returned None)

Score: 7

I think you're confused because you're running 11 from the REPL, which automatically prints 10 out the value returned when you call a function. In 9 that case, you do get identical output whether 8 you have a function that creates a value, prints 7 it, and throws it away, or you have a function 6 that creates a value and returns it, letting 5 the REPL print it.

However, these are very 4 much not the same thing, as you will realize 3 when you call autoparts with another function 2 that wants to do something with the value 1 that autoparts creates.

Score: 4

you just add a return statement...

def autoparts():
  parts_dict={}
  list_of_parts = open('list_of_parts.txt', 'r')
  for line in list_of_parts:
        k, v = line.split()
        parts_dict[k] = v
  return parts_dict

printing 4 out only prints out to the standard output 3 (screen) of the application. You can also 2 return multiple things by separating them 1 with commas:

return parts_dict, list_of_parts

to use it:

test_dict = {}
test_dict = autoparts()
Score: 3

Major difference:

Calling print will immediately make your program 3 write out text for you to see. Use print 2 when you want to show a value to a human.

return 1 is a keyword. When a return statement is reached, Python will stop the execution of the current function, sending a value out to where the function was called. Use return when you want to send a value from one point in your code to another.

Using return changes the flow of the program. Using print does not.

Score: 3

A function is, at a basic level, a block 24 of code that can executed, not when written, but 23 when called. So let's say I have the following 22 piece of code, which is a simple multiplication 21 function:

def multiply(x,y):
    return x * y

So if I called the function with 20 multiply(2,3), it would return the value 6. If I modified 19 the function so it looks like this:

def multiply(x,y):
    print(x*y)
    return x*y

...then 18 the output is as you would expect, the number 17 6 printed. However, the difference between 16 these two statements is that print merely shows 15 something on the console, but return "gives 14 something back" to whatever called 13 it, which is often a variable. The variable 12 is then assigned the value of the return 11 statement in the function that it called. Here 10 is an example in the python shell:

>>> def multiply(x,y):
        return x*y

>>> multiply(2,3) #no variable assignment
6
>>> answer = multiply(2,3) #answer = whatever the function returns
>>> answer
6

So now 9 the function has returned the result of 8 calling the function to the place where 7 it was called from, which is a variable 6 called 'answer' in this case.

This does much 5 more than simply printing the result, because 4 you can then access it again. Here is an 3 example of the function using return statements:

>>> x = int(input("Enter a number: "))
Enter a number: 5
>>> y = int(input("Enter another number: "))
Enter another number: 6
>>> answer = multiply(x,y)
>>> print("Your answer is {}".format(answer)
Your answer is 30

So 2 it basically stores the result of calling 1 a function in a variable.

Score: 1

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

I’ll 34 start with a basic explanation. print just 33 shows the human user a string representing 32 what is going on inside the computer. The 31 computer cannot make use of that printing. return 30 is how a function gives back a value. This 29 value is often unseen by the human user, but 28 it can be used by the computer in further 27 functions.

On a more expansive note, print 26 will not in any way affect a function. It 25 is simply there for the human user’s benefit. It 24 is very useful for understanding how a program 23 works and can be used in debugging to check 22 various values in a program without interrupting 21 the program.

return is the main way that 20 a function returns a value. All functions 19 will return a value, and if there is no 18 return statement (or yield but don’t worry 17 about that yet), it will return None. The 16 value that is returned by a function can 15 then be further used as an argument passed 14 to another function, stored as a variable, or 13 just printed for the benefit of the human 12 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 11 "Now let us see what the values of 10 f1 and f2 are"

print f1 --->None

print 9 f2---->"I returned"

When function_that_prints 8 ran, it automatically printed to the console 7 "I printed". However, the value 6 stored in f1 is None because that function 5 had no return statement.

When function_that_returns 4 ran, it did not print anything to the console. However, it 3 did return a value, and that value was stored 2 in f2. When we printed f2 at the end of 1 the code, we saw "I returned"

Score: 1

The below examples might help understand:

def add_nums1(x,y):
    print(x+y)

def add_nums2(x,y):
    return x+y 

#----Function output is usable for further processing
add_nums2(10,20)/2
15.0

#----Function output can't be used further (gives TypeError)
add_nums1(10,20)/2

30
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-124-e11302d7195e> in <module>
----> 1 add_nums1(10,20)/2

TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

0

Score: 0
def add(x, y):
    return x+y

That way it can then become a variable.

sum = add(3, 5)

print(sum)

But 3 if the 'add' function print the output 'sum' would 2 then be None as action would have already 1 taken place after it being assigned.

More Related questions