Understanding Return
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
What You Will Learn
- How to return a value from a method and a block
- The default return values
4 Cases
- Method with a return statement
- Method without a return statement
- Block without a return statement
- Block with a return statement
Returning a Value From a Method
Use the caret ^
to return a value from a method
Default Method's Return Value
A method with no caret ^
returns self
is equivalent to
Blocks Return Value
Blocks return the value of their last expression
The caret ^
in a block has a special meaning...
A Caret in a Block Returns from the Method
A caret ^
in a block quits the enclosing method
- When returning (with caret
^
) from a block, the method defining the block is terminated
- Further readings: http://deepintopharo.org
What you Should Know
- The caret
^
always terminates the method
- A method returns
self
by default
- A block returns the result of its last expression
/