Parameter passing means that in the process of passing the program, the actual parameter passes the parameter value to the corresponding formal parameter, and then implements the process of data processing and return in the function. The more common parameter passes are: value passing, passing parameters by address, or passing parameters by array.
1. Conventional delivery
When using a normal variable as a function parameter, the parameter is passed only as a copy of the value of the variable, and the value of the argument is copied to the variable parameter, and when the function handles the argument, it does not affect the value of the original argument.
Egg
Package Main
Import "FMT"
Func AA (A, b int) {
var temp int
temp = a
A = b
b = Temp
Fmt. Println (A, B)//handles the value of A/b interchange
}
Func Main () {
x: = 5
Y: = 10
AA (x, y)
Fmt. Println (x, y)//After swapping or printing the original value
}
Output Result:
C:/users/cherry/go/src/aa/aa.exe [C:/USERS/CHERRY/GO/SRC/AA]
10 5
5 10
Success: Process exit code 0.
2. Pointer function
The variables of a function can not only use ordinary variables, but also use pointer variables, when using pointer variables as parameters of a function, when parameter passing is an address, the memory address of the argument is copied to the variable parameter, then the change of the parameter will affect the value of the argument.
Egg
Package Main
Import "FMT"
Func AA (a *int, b *int) {
var temp int
temp = *a
*a = *b
*b = Temp
Fmt. Println (temp, *a, *b)
}
Func Main () {
x: = 5
Y: = 10
AA (&x, &y)
Fmt. Println (x, y)
}
Execution Result:
C:/users/cherry/go/src/aa/aa.exe [C:/USERS/CHERRY/GO/SRC/AA]
10 5
Success: Process exit code 0.
3. Array elements as function parameters
When using an array element as a parameter, it uses the same method as the normal variable, which is both a "copy of value"
Package Main
Import "FMT"
Func AA (a int) {
A + = 100
Fmt. Printf ("Print internal cumulative value:%d\n", a)
}
Func Main () {
var s = [...] Int{1, 2, 3, 4, 5, 6}
Fmt. Printf ("Start calling function .... \ n")
AA (S[3])
Fmt. Printf ("Call End ...")
Fmt. Println (S[3])
}
Execution Result:
C:/users/cherry/go/src/aa/aa.exe [C:/USERS/CHERRY/GO/SRC/AA]
Start calling function ....
After printing the internal cumulative value: 104
Call End ... 4
Success: Process exit code 0.
4, the array name as a function parameter: Unlike other languages, the go language when the name of the array as a function parameter, the parameter transfer is not only the copy of the array, in the formal parameters of the elements of the changes will not affect the original value of the elements, code as follows:
Package Main
Import "FMT"
Func AA (S [6]int) {
S[3] + = 100
Fmt. Printf ("Print internal cumulative value:%d\n", s)
}
Func Main () {
var s = [...] Int{1, 2, 3, 4, 5, 6}
Fmt. Printf ("Start calling function .... \ n")
AA (s)
Fmt. Printf ("Call End ...")
Fmt. PRINTLN (s)
}
Execution Result:
C:/users/cherry/go/src/aa/aa.exe [C:/USERS/CHERRY/GO/SRC/AA]
Start calling function ....
Print the internal cumulative value: [1 2 3 104 5 6]
Call End ... [1 2 3 4 5 6]
# #将数组作为参数传入时, you must specify the length of the incoming array as the initial definition length
5. Slice as function parameter
When using Sclice as a function parameter, the parameter passing will be an address copy, the memory address of the underlying array is copied to the parameter slice, the operation of the slice element is the operation of the underlying array element.
Egg
Package Main
Import "FMT"
Func aa (S1 []int) {
S1[0] + = 100
}
Func Main () {
var a = [5]int{1, 2, 3, 4, 5}
var s []int = a[0:]
AA (s)
Fmt. Println (S[0])
Fmt. Println (a)
}
Execution Result:
C:/users/cherry/go/src/aa/aa.exe [C:/USERS/CHERRY/GO/SRC/AA]
101
[101 2 3 4 5]//The value of the original array has changed
6, function as a parameter: In the Go language, functions also act as a data type, so functions can also be used as arguments to functions.
Package Main
Import "FMT"
Func AA (A, b int, bb func (int, int) int) {
Fmt. Println (BB (A, B))
}
Func BB (A, b int) int {
Return a + b
}
Func Main () {
var a, b int = 5, 6
f: = BB
Fmt. Println (f)
AA (A, B, f)
Fmt. Println (A, B, f)
}
The function bb is the formal parameter of the function AA, and the variable f is a function type, as an argument when the AA () is called
This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1963695
Go language: Explanation of function parameter passing