Go Learning notes: Functions

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Functions are an important part of the go language.

First, function definition

Package Mainimport "FMT"//This function calculates the and of two int input data and returns the int type and the Func plus (a int, b int) int {//GO need to return the value explicitly with return statement A + b}func main () {//functions are called in a simple way//"name (parameter list)" Res: = Plus (1, 2) fmt. Println ("1+2 =", res)}

The output result is

1+2 = 3

Here, the function can be defined in front of the main () function, or after main (). The following code is equivalent to the above:

Package Mainimport "FMT" Func Main () {///function is very simple to call//"name (parameter list)" Res: = Plus (1, 2) fmt. Println ("1+2 =", res)}//This function computes the and of two int input data and returns the int type and the Func plus (a int, b int) int {//GO needs to return the value explicitly with return a + B}



Second, function named return value

The function accepts parameters. In Go, a function can return more than one result parameter, not just a value. They can be named and used like variables.

If the return value parameter is named, a statement with no arguments return returns the current value as the return value. Note that if you encounter a code block with the same name as a return value, you also need to explicitly write out the return value.

Package Mainimport "FMT" func split (sum int) (x, y int) {x = sum * 4/9 y = sum-x return}func main () (FMT). Println (Split (17))}

Run results

7 10














Related Article

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.