+ 1
#print vs. return. What is the difference between print and return in a function?
Can we use only print or only return in a function? Or each of them should be used for specific things? Please explain!
11 ответов
+ 4
Thomas Williams, I fully agree with you. But to be honest, the search in the app is not very good. Please try to find relevant answers by using "python print return". The result is not as you can it expect today. You will get a bunch of hits meaning only python or only print and so on. And there is no sorting on relevance in the result list.
So there is no really benefit in using it. Sorry to say. If i am wrong, it would be nice if you can correct me, and please also tell me if there is something wrong how i do the search. Searching in browser with bing, google or duckduckgo is much more faster and gives a better result as inside the app.
+ 3
Thomas Williams, what was your search string exactly? From my experience about searching inside the app, i have my doubts about the list you presented here. I am sure you have been scrolling around and pick some relevant answers together.
+ 3
Thomas Williams, This are the top 10 hits in the list if I use "Print vs Return" and setting is "Most Recent. Except of the actual feed we are just in now, not a single one has any similarity to what I am looking for. So you are kidding me?
https://www.sololearn.com/Discuss/2037614/?ref=app
https://www.sololearn.com/Discuss/2037573/?ref=app
#This is the actual feed:
https://www.sololearn.com/Discuss/2037439/?ref=app
https://www.sololearn.com/Discuss/2037153/?ref=app
https://www.sololearn.com/Discuss/2036774/?ref=app
https://www.sololearn.com/Discuss/2036746/?ref=app
https://www.sololearn.com/Discuss/2036460/?ref=app
https://www.sololearn.com/Discuss/2036448/?ref=app
https://www.sololearn.com/Discuss/2036422/?ref=app
https://www.sololearn.com/Discuss/2035989/?ref=app
+ 2
print() is a function that displays something on the screen
return is a keyword that yeets a value out of a function, breaking it in the progress
You use print() to show stuff, you use return to store results in variables etc.
Consider this
def add_nums(numA, numB):
return numA + numB
def add_nums2(numA, numB):
print(numA + numB)
result_return = add_nums(2,3)
print_result = add_nums2(4,5) # prints value but returns nothing
print(result_return) # value = 5
print(print_result) # value = None
+ 2
Thomas Williams thanks for your explanation, now is answered millions and one of times :)
+ 1
This question has been asked AND answered MILLIONS of times. Next time, use the search bar
+ 1
Thomas Williams honestly your answer from here is friendlier that these searches, so maybe sometimes is ok to ask for better examples
+ 1
"Print vs Return"
+ 1
Well I did use "Most Popular". I admit, they werent the top 5, but they were the top 20
0
Just searched Print vs Return
https://www.sololearn.com/discuss/658299/?ref=app
https://www.sololearn.com/discuss/1912586/?ref=app
https://www.sololearn.com/discuss/1524048/?ref=app
https://www.sololearn.com/discuss/243524/?ref=app
https://www.sololearn.com/discuss/48824/?ref=app
https://www.sololearn.com/discuss/1142332/?ref=app
https://www.sololearn.com/discuss/241342/?ref=app
https://www.sololearn.com/discuss/1476808/?ref=app
0
# difference between print and return
#1.'return' store the value in function so to get the output you have to use 'print'.
#2.but if we use print instead of return then we can simply call the function to get the output with using print function.
#3. if return is used anything below this within its indentation will not be valid.
def add_numbers(x, y):
total = x + y
print (total)
print("This won't be printed")
add_numbers(4, 5)
# output >9
#>This won't be printed
def add_numbers(x, y):
total = x + y
return total
print("This won't be printed")
print(add_numbers(4,5))
# output >9
# please note function does not give any output if return is used
def add_numbers(x, y):
total = x + y
return total
print("This won't be printed")
add_numbers(4,5)
# output >
#blank, as we have used return only calling the function wont work we have to go for print