Skip to content

Latest commit

 

History

History
62 lines (42 loc) · 953 Bytes

File metadata and controls

62 lines (42 loc) · 953 Bytes

Names Return Values

Objectives

  1. 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))
}
  1. Fix the code above

  2. 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))
}

Solution

  1. 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))
}
  1. Yes, it's known as "naked return". It's a return statement without any arguments that returns the named arguments (in this case z)