A value created inside a function can't be used outside of it unless you
Consider the following function:
If we wanted to store 5 squared in a variable called
def square(n): return n * n
x, we would do:
x = square(5).
xwould now equal
Things to note
>>> def square(n): ... n * n # calculates then throws away, returns None ... >>> x = square(5) >>> print(x) None >>> def square(n): ... print(n * n) # calculates and prints, then throws away and returns None ... >>> x = square(5) 25 >>> print(x) None
returndo not accomplish the same thing.
print()will show the value, and then it will be gone.
- A function will return
Noneif it ends without a
- When you want to print a value from a function, it's best to return the value and print the function call instead, like