Home > Article > Backend Development > Go language basics-function
What is a function?
#A function is a block of code that performs a specific task. A function takes input, performs some calculations on the input, and produces an output.
The Go language function declaration syntax is as follows:
func functionname(parametername type) returntype { //function body }
The function declaration starts with the func keyword, followed by the function name. Parameters are specified within square brackets (), followed by the return type of the function. The syntax for specifying parameters is the parameter name followed by the parameter type. You can specify any number of parameters, like the following:
(parameter1 type, parameter2 type)
The main part of the function is placed inside curly brackets.
The parameters and return value are optional, so the following declaration is also a valid function declaration:
func functionname() { }
Let's write a function. The parameters of the function are the unit price of the product and the quantity of the product. It calculates the total price of the product and returns it.
func calculateBill(price int, no int) int { var totalPrice = price * no return totalPrice }
The input parameters of the above function are price and no, both of type int. The product of the two is assigned to totalPrice and returned as the return value. totalPrice is also of type int.
If consecutive parameters are of the same type, we can avoid specifying the type for each parameter and only need to specify the type of the last parameter, such as price int, no int It can be written as price, no int, so the above function can be modified as:
func calculateBill(price, no int) int { var totalPrice = price * no return totalPrice }
Now that we have written a function, we try to call this function. The syntax for calling a function is functionname(parameters). The above function can be called as follows:
calculateBill(10, 5)
The following is the completed program:
package main import ( "fmt" ) func calculateBill(price, no int) int { var totalPrice = price * no return totalPrice } func main() { price, no := 90, 6 totalPrice := calculateBill(price, no) fmt.Println("Total price is", totalPrice) }
Execution [1]
The above code output:
Total price is 540
Go 语言是允许函数返回多个值的。我们来写一个 rectProps() 函数,该函数的参数是长方形的 length 和 width,返回长方形的 area(面积) 和 perimeter(周长)。
package main import ( "fmt" ) func rectProps(length, width float64)(float64, float64) { var area = length * width var perimeter = (length + width) * 2 return area, perimeter } func main() { area, perimeter := rectProps(10.8, 5.6) fmt.Printf("Area %f Perimeter %f", area, perimeter) }
执行[2]
如果一个函数返回多个值则需要使用 () 指定,比如:
func rectProps(length, width float64)(float64, float64)
该函数有两个 float64 参数 length 和 width,也返回两个 float64 类型的值。
上面的代码输出:
Area 60.480000 Perimeter 32.800000
可以从函数返回命名值。如果返回值被命名,则可以认为它在函数的第一行被声明为变量,并初始化为相应类型的零值。
上面的 rectProps() 函数可以使用命名的返回值重写:
func rectProps(length, width float64)(area, perimeter float64) { area = length * width perimeter = (length + width) * 2 return //no explicit return value }
area 和 perimeter 是函数中命名的返回值。
需要注意的是,函数里的 return 语句没有返回任何值。由于在函数声明中将 area 和 perimeter 指定为返回值,因此当遇到 return 语句时,它们会自动从函数中返回。
Go 语言里面,下划线 _ 可以被当做空白符使用。它可以用来代替任何类型的任何值,让我们看看这个空白标识符有什么用。
rectProps() 函数返回长方形的面积和周长。如果我们只想要面积,而不想要周长那该怎么办呢?这时候空百符 _ 就可以派上用场了。
package main import ( "fmt" ) func rectProps(length, width float64) (float64, float64) { var area = length * width var perimeter = (length + width) * 2 return area, perimeter } func main() { area, _ := rectProps(10.8, 5.6) // perimeter is discarded fmt.Printf("Area %f ", area) }
执行[3]
上面代码的第 13 行,我们接收了函数返回的 area,并使用 _ 忽略了 perimeter。
The above is the detailed content of Go language basics-function. For more information, please follow other related articles on the PHP Chinese website!