Golang Learning notes-1.6 functions

Source: Internet
Author: User

This is the sixth chapter of Golang Language Learning Tutorial

declaring functions

Grammar

func name(parametername type) returntype {    //函数体(具体实现的功能)}

function declarations include:
namefunction Name: Custom functions Name
Formal parameter list () : () defined, describing the function's parameter name and parameter type
Return values list rerurntype : Variables that describe the return value of a function and the type
function Body {} : The code that implements the function concretely

function declaration Interpretation
The declaration of a function begins with a keyword, followed by func the name of the function you define. name(函数名)
The parameter list of the function is defined () between the
The type of the return value is defined in the returntype (return value list),
The part that {} is contained is the body of the function.
Note: The syntax for declaring a parameter is the parameter type , which declares that multiple parameters are in the form of parameter types, parameter types , and argument names.

The argument list and the return value in the function are not required and can be omitted.
Example: the following function declaration is also valid

func testfunc(){    //此函数不需要输入参数,且没有返回值}

Example functions

//平分糖果的函数,输入参数是总共的糖果数量和人的数量,两者的商为每人分的数量,作为返回值func Candy(total int, people int) int {  //参数类型相同时可写成(total, people int)    var avg = total / people  //平均值 = 总数  /  人数    return avg  //返回平均值}

The above has two integer input, total and, the people return value is avg , is also the integer type.
Now that you have defined a function, you need to call this function in your program. The syntax for calling a function is: name(parametername) , as follows:

Candy(20, 5)

After completing the declaration and invocation of the function, you can write out a complete program that will give everyone the candy output:

package mainimport "fmt"//平分糖果的函数,输入参数是总共的糖果数量和人的数量,两者的商为每人分的数量,作为返回值func Candy(total int, people int) int {  //参数类型相同时可写成(total, people int)    var avg = total / people    return avg}func main(){    avg := Candy(20, 5)    fmt.Println("Everyone can get", avg)}

The result of the above program operation is:

Everyone can get 4

Multiple return values

The go language supports a function that has multiple return values.
Cases:

package mainimport (      "fmt")//求长方形周长和面积的函数,输出参数是长和宽,周长是长和宽之和的两倍,面积是长和宽的乘积func rectangle(length,width int)(int, int) {    var perimeter= (length + width) * 2    var area= length * width    return perimeter, area}func main (){    perimeter, area := rectangle(20, 5)    fmt.Println("the perimeter is", perimeter, "and the area is", area)}

If a function has more than one return value, it must be enclosed in (), and the two return values in the above program are respectively perimeter and area .
The result of the above program operation is:

The perimeter is 100

If you want to invoke only one return value, replace it with another return value _ :
perimeter, _ := rectangle(20, 5)

Named return value

A return value can be named from a function, and once the return value is named, it can be assumed that the value is declared in the first line of the function and is not required to be declared in the body of the function.
The above rectangle function can be written as:

func rectangle(length,width int)(perimeter, area int) { //在第一行中命名返回值,声明变量    perimeter= (length + width) * 2    area= length * width    return  //不需要指定返回值,默认返回 perimeter, area}

Comparing the two methods above, the named return value rerurntype declares the return value in the return value list segment and does not need to declare the variable in the body of the function, directly equals the value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.