I am trying to understand the basics of scala curly braces and with/without return statement.
def method(a:Int):Int = {
if(a == 10 ) 10
if(a == 11 ) {11}
if(a == 12 ) { return 12 }
if(a == 13 ) return {13}
1}
Method Call | Output |
---|---|
method(10) | 1 |
method(11) | 1 |
method(12) | 12 |
method(13) | 13 |
From the above we can note that
- the output of calls to method(12) & method(13) are as expected.
- Whereas the output of calls to method(10) & method(11) we see the value returned is 1 which is confusing given return keyword is not required for scala.
Another thing I noted was that :
def method(a:Int) = {
if(a == 10 ) 10
if(a == 11 ) {11}
if(a == 12 ) {12}
if(a == 13 ) {13}
}
Method Call | Output |
---|---|
method(10) | () |
method(11) | () |
method(12) | () |
method(13) | 13 |
The above two method statements seems confusing. Kindly help to clarify the above why there is difference in values returned.
Thanks in advance.