warriorprincess
05/28/2018, 12:36 PMelse if
work though?diesieben07
05/28/2018, 12:39 PMfun myFun(input: Int): Int {
if (input < 0) {
return -1
} else if (input > 0) {
return 1
}
}
val value = myFun(0)
what should value
be? It's not defined in the function. That's why the function does not compile.Int
, but for some cases there is no return value.fun myFun(input: Int): Int {
return if (input < 0) {
-1
} else if (input > 0) {
1
}
}
Czar
05/28/2018, 12:41 PMif
tree, you have to make sure that all branches return (exhaust all conditions).warriorprincess
05/28/2018, 12:44 PMdiesieben07
05/28/2018, 12:45 PMelse
block, even if your else if
-chain already covers all cases.warriorprincess
05/28/2018, 12:49 PM