+ 1
Why 'return' is used instead of 'print' ?
6 ответов
+ 4
Return is used mainly in functions/methods :
'''
This function do a simple search inside an iterable object (where) and return an object (what) if it find it inside "where" else None is returned
'''
def search(where, what):
for curr in where:
if curr == what:
# here we can stop this function
# without continue it
return curr
return #implict None
objects=['pen', 'table', 'paper']
# out: 'table'
print(search(objects, 'table'))
# out: None
print(search(objects, 'car'))
+ 3
Their have different goals.
"return" is a language constuct used inside a function/method for return a value (if any) from the current function/method to caller function/method then stopping current function/method execution .
"print" is a a function than print to console some data
+ 2
Thanks for your help, i got it now..☺️
+ 2
👍👍👍
+ 1
What is the need for stopping a method that is defined?..
For eg:
total=x+y
Return total #it gives use the output, but stops the method..
#but if we use print..
print(total). # it doesn't stop the method..and we can use that method further..
So what's the need to stop a method?
+ 1