Golang: the return value of the function name

You can give a return value specifies the name of the function. If the return value of a name specified, it is treated as a defined variable names in the first line of the function.

Let's write a function rectProps, it accepts the length and width of a rectangle, and returns the rectangle of area and perimeter.

package main

import (
    "fmt"
)

func rectProps(length, width float64) (area, perimeter float64) {
    area = length * width
    perimeter = (length + width) * 2
    return //no explicit return value
}

func main() {
    area, perimeter := rectProps(10.8, 5.6)
    fmt.Printf("Area %f Perimeter %f", area, perimeter)
}

In the above function, area and perimeter are named return value. Note that the return statement does not specify any return value. As it has been in the designated area and the perimeter is the return value of the function declaration, they will automatically return from the function in the event of a return statement.

In Golang, there is the function returns a value, whether it is named return value or the ordinary form of the return value of the function must include a return statement.

Author: CD Dance with python
link: https: //www.jianshu.com/p/ce58bc8885e2
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Published 48 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_37825371/article/details/103985592