函数返回值
1 func haha(传值) (返回值) {....}
2 func haha(a, b int) (sum int) {....}
一个返回值函数
1 package main
2
3 import "fmt"
4
5 func main() {
6 //把sum函数的返回值,传到一个变量中
7 sum1 := sum(3, 4)
8 fmt.Println(sum1)
9
10 }
11
12 //返回int值
13 func sum(value1, value2 int) int {
14 value3 := value1 + value2
15 return value3
16 }
预定返回值名字
1 package main
2
3 import "fmt"
4
5 func main() {
6 //把sum函数的返回值,传到一个变量中
7 sum1 := sum(3, 4)
8 fmt.Println(sum1)
9
10 }
11
12 //返回值起名为he
13 func sum(value1, value2 int) (he int) {
14
15 //这he对应好 返回值he
16 he = value1 + value2
17
18 //不用写sum 自然知道return它
19 return
20 }
多个返回值函数
1 package main
2
3 import "fmt"
4
5 func main() {
6
7 jia, jian, cheng, chu := yun_suan(3, 4)
8 fmt.Println(jia, jian, cheng, chu)
9
10 }
11
12 //为数据运算加减乘除定义多个返回数值
13 func yun_suan(value1, value2 int) (jia, jian, cheng, chu int) {
14
15 jia = value1 + value2
16 jian = value1 - value2
17 cheng = value1 * value2
18 chu = value1 / value2
19
20 return jia, jian, cheng, chu
21 }