- What is the result of the following code?
package main
import "fmt"
func add(x, y int) {
var z int = x + y
return z
}
func main() {
fmt.Print(add(3, 2))
}
-
Fix the code above
-
Will the following code work properly? Explain
package main
import "fmt"
func add(x, y int) (z int) {
z = x + y
return
}
func main() {
fmt.Print(add(3, 2))
}
-
It will fail since the function defined to not return any value and it returns an integer.
package main
import "fmt"
func add(x, y int) (z int) {
z = x + y
return z
}
func main() {
fmt.Print(add(3, 2))
}
- Yes, it's known as "naked return". It's a return statement without any arguments that returns the named arguments (in this case
z
)