Browse Source

函数声明

YWJL 2 years ago
parent
commit
f1446c324d
3 changed files with 48 additions and 9 deletions
  1. BIN
      Go语言101/golang学习路线.png
  2. 25 9
      Go语言101/hello.go
  3. 23 0
      Go语言101/入门.md

BIN
Go语言101/golang学习路线.png


+ 25 - 9
Go语言101/hello.go

@@ -1,8 +1,8 @@
 package main
 
 import (
+	"fmt"
 	"math/rand"
-	"reflect"
 ) //引入math/rand标准库包,并以rand作为引入名
 
 const MaxRand = 16 // 声明一个具名整型常量
@@ -26,20 +26,36 @@ func StatRandomNumbers(numRands int) (int, int) {
 	return a, b // 此函数返回两个结果
 }
 
+func SquareesofSumAndDiff(a, b int64) (s, d int64) {
+	// return (a + b) * (a + b), (a - b) * (a - b)
+	s = (a + b) * (a + b)
+	d = (a - b) * (a - b)
+	return
+
+}
+
 // main函数,或主函数,是一个程序的入口函数。
 func main() {
-	var num = 100
-	// 调用上面声明的StatRandomNumbers函数,
-	// 并将结果赋给使用短声明语句声明的两个变量。
+	{
+		// part1
+		// var num = 100
+		// 调用上面声明的StatRandomNumbers函数,
+		// 并将结果赋给使用短声明语句声明的两个变量。
 
-	x, y := StatRandomNumbers(num)
+		// x, y := StatRandomNumbers(num)
 
-	// 调用两个内置函数(print和println)。
+		// 调用两个内置函数(print和println)。
 
-	print("Result: ", x, " + ", y, " = ", num, "? ")
+		// print("Result: ", x, " + ", y, " = ", num, "? ")
 
-	println(x+y == num)
+		// println(x+y == num)
 
-	println(reflect.TypeOf("num").Name())
+		// println(reflect.TypeOf("num").Name())
+	}
 
+	{
+		// 函数声明与调用
+		x, y := SquareesofSumAndDiff(1, 2)
+		fmt.Printf("%d,%d", x, y)
+	}
 }

+ 23 - 0
Go语言101/入门.md

@@ -496,7 +496,30 @@ Go语言有两种变量声明形式。一种称为标准形式,另一种称为
 
 ### 函数声明
 
+标准的函数声明
 
+```go
+1| func SquaresOfSumAndDiff(a int64, b int64) (s int64, d
+int64) {
+2| x, y := a + b, a - b
+3| s = x * x
+4| d = y * y
+5| return // <=> return s, d
+6| }
+
+```
+
+Golang 函数不支持入参默认值;参数是默认值是其类型的零值。
+
+连续输入的参数类型共用
+
+```go
+1| func SquaresOfSumAndDiff(a, b int64) (s, d int64) {
+2| return (a+b) * (a+b), (a-b) * (a-b)
+3| // 上面这行等价于下面这行:
+4| // s = (a+b) * (a+b); d = (a-b) * (a-b); return
+5| }
+```