+ 3
What is the difference of Return and Print?
What is the purpose of return? Wouldn't print just be easier?
2 Respostas
+ 3
return gets you variables from the local namespace of functions so you can use them in your global namespace.
# local function namespace
def add(a, b):
res = a + b
return res
#global namespace
a = 1
b = 2
sum = add(a, b)
print(sum)
output:
3
print() prints the objects you provided, for instance a string and/or variables, to your screen.
def foo(bar):
print("Hello")
print("World)
output:
"Hello"
"World"
+ 1
Kevin Dietrichstein So return is like print for functions?