Difference between returns and printing in python? [duplicate]

The Point

return is not a function. It is a control flow construct (like if else constructs). It is what lets you “take data with you between function calls”.

Break down

  • print: gives the value to the user as an output string. print(3) would give a string '3' to the screen for the user to view. The program would lose the value.

  • return: gives the value to the program. Callers of the function then have the actual data and data type (bool, int, etc…) return 3 would have the value 3 put in place of where the function was called.

Example Time

def ret():
    return 3

def pri():
    print(3)

4 + ret() # ret() is replaced with the number 3 when the function ret returns
# >>> 7
4 + pri() # pri() prints 3 and implicitly returns None which can't be added
# >>> 3
# >>> TypeError cannot add int and NoneType

Leave a Comment