May 11, 2021 Go
A function is a basic block of code that is used to perform a task.
The Go language has at least one main() function.
You can divide different functions by functions, and logically each function performs the specified task.
The function declaration tells the compiler the name of the function, the return type, and the parameters.
The Go Language Standard Library provides a variety of built-in functions that are available. F or example, a len() function can accept different type parameters and return the length of that type. If we pass in a string, we return the length of the string, and if we pass in a number, the number of functions contained in the array is returned.
The Go language function is defined in the following format:
func function_name( [parameter list] ) [return_types]{ 函数体 }
Function definition parsing:
The following example is the code for the max() function, which passes in two integer parameters, num1 and num2, and returns the maximum values for both parameters:
/* 函数返回两个数的最大值 */ func max(num1, num2 int) int{ /* 声明局部变量 */ result int if (num1 > num2) { result = num1 } else { result = num2 } return result }
When you create a function, you define what the function needs to do and perform the specified task by calling the change function.
Call the function, pass arguments to the function, and return values, such as:
package main import "fmt" func main() { /* 定义局部变量 */ var a int = 100 var b int = 200 var ret int /* 调用函数并返回最大值 */ ret = max(a, b) fmt.Printf( "最大值是 : %d\n", ret ) } /* 函数返回两个数的最大值 */ func max(num1, num2 int) int { /* 定义局部变量 */ var result int if (num1 > num2) { result = num1 } else { result = num2 } return result }
The above example calls the max() function in the main() function, and the result is:
最大值是 : 200
The Go function can return multiple values, such as:
package main import "fmt" func swap(x, y string) (string, string) { return y, x } func main() { a, b := swap("Mahesh", "Kumar") fmt.Println(a, b) }
The above examples perform as follows:
Kumar Mahesh
If a function uses an argument, the variable can be called a parameter of a function.
A parameter is like a local variable defined in a function body.
A function can be called to pass arguments in two ways:
The delivery type | Describe |
---|---|
The value is passed | Value passing is the transfer of a copy of the actual argument to the function when the function is called, so that if the argument is modified in the function, the actual argument is not affected. |
Reference pass | Reference passing refers to passing the address of an actual argument to a function when it is called, and the modifications made to the argument in the function affect the actual argument. |
By default, the Go language uses value delivery, which means that the actual parameters are not affected during the call.
Function usage | Describe |
---|---|
The function is the value | Once defined, the function can be used as a value |
Closure | Closures are anonymous functions that can be used in dynamic programming |
Method | A method is a function that contains the recipient |