Can yield and return be used in the same function? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Can yield and return be used in the same function?

14th Aug 2016, 4:34 AM
csestudent
3 Réponses
+ 2
yield is a form of function return that saves the state or scope of the function for re-entry. With a return from a function, all values are lost on exit. Not sure how two would react, but should be mutually exclusive in well written code. hth To me this is just a fast object method shortcut..
15th Aug 2016, 1:20 AM
returnSuccess
+ 1
I wrote a little code to test this and was surprised I did not get any error. Try checking out my Code Playground, but I saw my (function/generator) return a pointer to the result of a yield each time.
3rd Nov 2016, 11:01 PM
Ben Caffrey
- 1
Yes, they can, and when you do it, the value returned will be the "value" of the StopIteration exception after you exhaust your generator
17th Feb 2017, 4:18 AM
Amaras A
Amaras A - avatar