3 ms·
you can do func sum(nums []int) (result int) { for _, n := range nums { result += n } return } and it will work sa
by ilyt 4y ago
you can do
func sum(nums []int) (result int) {
for _, n := range nums {
result += n
}
return
}
and it will work same as if you would do
return result
As for omitting return entirely I hated it in every language where I saw it. It just feels wrong to not have return in functions that return stuff
- jdmichal 4y agoI think missing `return` works if the language is designed around everything being expressions, so the function is just written as a single expression. I agree that in procedural type paradigms I like having the `return` keyword over things like "return the result of the last expression".
- int_19h 4y agoThis case is a bit different - omitting the return simply returns the implicitly declared result variable, which makes perfect sense.