print() vs return
In this lesson we will be examining the differences between using a print()
statement and a return
statement
Example Function: Average of Integers in a List
print()
vs return
in our example
print()
vs return
in our exampleCalling the function without variable assignment:
When we first called averagePrint1(values)
, it outputted the message with the result
When we first called averagePrint2(values)
, it did not output anything
This is the difference of using print() and return: print() outputs a message to the console whereas return does not
Assigning variables with the result of a function call:
When we output the variable result1
the value is None
.
When we output the variable result2
the value is the resulting average.
This happens because the function: averagePrint1 does not return any value; therefore, variable result1 cannot be assigned with the result from said function
The function, averagePrint2, returns the average variable; therefore, it can be used for variable assignment
NOTE: 9 out of 10 times your functions should and will return a value
Last updated