Hi, I’m @Luo Zhu

This article was first published on luo Zhu’s official website

This article was translated from Golang Tutorial Series

This article synchronizes in the public account Luo Zhu early teahouse, reprint please contact the author.

Creation is not easy, form a habit, quality three even!

What is the function?

A function is a block of code that performs a specific task. A function takes an input, performs some calculations on that input, and then generates an output.

Function declaration

The syntax for declaring a function in the Go language is:

func functionname(parametername type) returntype {
 / / the function body
}
Copy the code

Function declarations begin with the keyword func, followed by functionname. Arguments are specified between functions (and), followed by the function’s returnType. The syntax for specifying parameters is the parameter name followed by the type. You can specify any number of parameters, for example, (parameter1 type, parameter2 type). Then there is a block of code between {and}, which is the body of the function.

Arguments and return types are optional in functions. Therefore, the following syntax is also a valid function declaration.

func functionname(a){}Copy the code

The sample

Let’s write a function that takes the price and quantity of a single product as input parameters, calculates the total price and returns the output by multiplying the two values.

func calculateBill(price int, no int) int {
    var totalPrice = price * no
    return totalPrice
}
Copy the code

The above function takes two input arguments of type int price and no and returns totalPrice, the product of price and no. The return value is also an int.

If continuous arguments have the same type, you don’t have to write the type twice, in fact you only need to write it once. Therefore, the above functionality can be rewritten as:

func calculateBill(price, no int) int {
    var totalPrice = price * no
    return totalPrice
}
Copy the code

Now we have a function ready to call from somewhere in our code. The syntax for calling a function is functionName (parameters). Now we can use code to call the above function.

calculateBill(10.5)
Copy the code

This is the complete program that uses the above function and prints the total price.

package main

import (
    "fmt"
)

func calculateBill(price, no int) int {
    var totalPrice = price * no
    return totalPrice
}

func main(a) {
    price, no := 90.6
    totalPrice := calculateBill(price, no)
    fmt.Println("Total price is", totalPrice)
}
Copy the code

Run in playground

The above program will print the following:

Total price is 540
Copy the code

Multiple return values

Multiple values can be returned from a function. Let’s write a function, rectProps, that takes the length and width of a rectangle and returns its area and perimeter. The area of a rectangle is the product of its length and width, and its perimeter is twice the sum of its length and width.

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(a) {
    area, perimeter := rectProps(10.8.5.6)
    fmt.Printf("Area %f Perimeter %f", area, perimeter)
}
Copy the code

Run in playground

If a function returns multiple return values, their types must be specified between (and). Func rectProps(Length, Width Float64)(float64,float64) has two float64 parameters length and width, and also returns two float64 values. The above program will print:

Generating Area 60.480000 32.800000Copy the code

Named return value

You can return named values from functions. If the return value is named, it can be treated as if it were declared as a variable in the first line of the function.

The above rectProps function can be rewritten with the named return value as

func rectProps(length, width float64)(area, perimeter float64) {
    area = length * width
    perimeter = (length + width) * 2
    return // Do not specify a return value explicitly
}
Copy the code

Area and perimeter are named return values in the above functions. Note that the return statement in the function does not explicitly return any value. Because you specified area and perimeter as return values in the function declaration, they are automatically returned from the function when a return statement is encountered.

Blank identifier

_ is a blank identifier in the Go language. It can replace any value of any type. Let’s see what this blank identifier is for.

The rectProps function returns the area and perimeter of the rectangle. What if we only need area and want to discard perimeter? This is where _ comes in handy.

The following program uses only the area returned from the rectProps function.

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(a) {
    area, _ := rectProps(10.8.5.6) // perimeter is discarded
    fmt.Printf("Area %f ", area)
}
Copy the code

Run in playground

In line 13, we use only area and use the _ identifier to discard perimeter.