package main
import "fmt"
func main() {
sum := sumFn(2, 10)
fmt.Println(sum)
}
func sumFn(x, y int) int {
return x + y
}
函数的可变参数
package main
import "fmt"
func main() {
sumN := sumNfn(10, 20, 30, 5)
fmt.Println(sumN)
}
func sumNfn(x ...int) int {
var str int
for _, v := range x {
str += v
}
return str
}
有多个返回值,需要用括号括起来
package main
import "fmt"
func main() {
a, b := getMoreValue(10, 14)
fmt.Println(a, b)
}
func getMoreValue(x, y int) (int, int) {
sum := x + y
sub := x - y
return sum, sub
}
返回值命名
package main
import "fmt"
func main() {
a, b := getMoreValue(10, 14)
fmt.Println(a, b)
}
func getMoreValue(x, y int) (sum int, sub int) {
sum = x + y
sub = x - y
return sum, sub
}