#print vs. return. What is the difference between print and return in a function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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!

15th Oct 2019, 8:27 AM
edward 🇷🇴
edward 🇷🇴 - avatar
11 Answers
+ 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.
15th Oct 2019, 9:20 AM
Lothar
Lothar - avatar
+ 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.
15th Oct 2019, 9:39 AM
Lothar
Lothar - avatar
+ 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
15th Oct 2019, 8:48 AM
Trigger
Trigger - avatar
+ 2
Thomas Williams thanks for your explanation, now is answered millions and one of times :)
15th Oct 2019, 9:02 AM
edward 🇷🇴
edward 🇷🇴 - avatar
+ 1
This question has been asked AND answered MILLIONS of times. Next time, use the search bar
15th Oct 2019, 8:50 AM
Trigger
Trigger - avatar
+ 1
Thomas Williams honestly your answer from here is friendlier that these searches, so maybe sometimes is ok to ask for better examples
15th Oct 2019, 9:33 AM
edward 🇷🇴
edward 🇷🇴 - avatar
+ 1
"Print vs Return"
15th Oct 2019, 9:54 AM
Trigger
Trigger - avatar
+ 1
Well I did use "Most Popular". I admit, they werent the top 5, but they were the top 20
15th Oct 2019, 12:09 PM
Trigger
Trigger - avatar
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
16th Dec 2019, 5:30 PM
Sunando Adhikary
Sunando Adhikary - avatar