+ 1

Why 'return' is used instead of 'print' ?

28th Jun 2019, 5:30 AM
Amit Dubey
Amit Dubey - avatar
6 Antworten
+ 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'))
28th Jun 2019, 5:51 AM
KrOW
KrOW - avatar
+ 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
28th Jun 2019, 5:37 AM
KrOW
KrOW - avatar
+ 2
Thanks for your help, i got it now..☺️
28th Jun 2019, 5:54 AM
Amit Dubey
Amit Dubey - avatar
+ 2
👍👍👍
28th Jun 2019, 5:54 AM
KrOW
KrOW - avatar
+ 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?
28th Jun 2019, 5:42 AM
Amit Dubey
Amit Dubey - avatar
+ 1
28th Jun 2019, 5:42 AM
Amit Dubey
Amit Dubey - avatar