0
Can somebody explain the difference between return and print?
4 Respostas
+ 12
if you have a function return literally returns the value from it
Example:
def my_power(x):
return x*x*x # x^3
Then when we will call it:
p = my_power(3) # Will assign 3^3 to p
Then we can do whatever we want.
print - just displays value of sceen
exaple: print(3) # Will write 3 in console
+ 4
A small usage of return above print in a function is that if you further want to work on the value returned by a function(like passing the result of one function to another function) then it's better to return the result from the function instead of printing it over there... if u have nothing to do with the result of a function other than printing it to console then print can be used.
+ 1
print:Prints something to screen
return:Not sure it was returning a value???
+ 1
Though explanations provided by the sir Michael and Sir Kiran is best. BUT If you are a NON PROGTAMMER by profession or STILL DON'T UNDERSTAND, let me explain their points practically. Consider YOU and YOUR FRIEND attend a lecturer from Teacher A and B respectively.
CASE 1: TEACHER A teaching you
Teacher A is writing on board and as well as explaining you what he is teaching and teacher B teaching to your friend just write on board he never explain anything. Now the first teacher act as Return statement, you can explain to anyone without any difficulty and return the input feed to you by the teacher (or output returns by the teacher) to anyone.
Case 2: Teacher B teaching your friend
Your friend can only cram those things what he write on board and can top the exam but later on while explaining to someone he might stuck somewhere. The Teacher B and your friend act like a print function. They can write on board or paper but can't explain to someone. After reading this again definitely read sir Michael and Sir Kiran answer. :)