This is a creation in Article, where the information may have evolved or changed.
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, when passing a parameter it is only worth copying to 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.
For example:
Package Main
Import (
"FMT"
)
Func swap (a int, b int) {
var temp int
temp = a
A = b
b = Temp
}
Func Main () {
x: = 5
Y: = 10
Swap (x, y)
Fmt. Print (x, y)
}
Output:5
The value passed to swap is x,y is worth copying, and the function exchanges the copied values, but does not change the value of x, y .
2. Pointer passing
function variables can not only use ordinary variables, you can also use pointer variables, using pointer variables as parameters of the function, when the parameter is passed will be an address to see Bai, the actual parameter of the memory address is copied to the variable parameter, then the change of the parameter will affect the value of the argument.
We still use the above example, slightly modified as follows:
Package Main
Import (
"FMT"
)
Func swap (a *int, b *int) {
var temp int
temp = *a
*a = *b
*b = Temp
}
Func Main () {
x: = 5
Y: = 10
Swap (&x, &y)
Fmt. Print (x, y)
}
Output:5
3. Array elements as function parameters
When using an array element as a function parameter, it is used the same way as a normal variable, which is a "copy of value".
Cases:
Package Main
Import (
"FMT"
)
Func function (a int) {
A + = 100
}
Func Main () {
var s = [5]int{1, 2, 3, 4, 5}
function (S[2])
Fmt. Print (S[2])
}
Output results:3
You can see that the value of the array element S[2] is the argument of the function, and no matter what action is taken on the parameter, the arguments do not change.
4. Array name as function parameter
Unlike other languages, thego language is a copy of an array when it is used as a function parameter. Modification of an array element in a formal parameter does not affect the original value of the element. This is similar to the above, do not post code, interested in writing code to test it.
5. Slice as function parameter
When using slice as a function parameter, the parameter pass is an address copy that copies the memory address of the underlying array to the parameter slice. At this point, the operation of the slice element is the operation of the underlying array element . For example:
Package Main
Import (
"FMT"
)
Func function (S1 []int) {
S1[0] + = 100
}
Func Main () {
var a = [5]int{1, 2, 3, 4, 5}
var s []int = a[:]
function (s)
Fmt. Println (S[0])
}
Running Result:101
6. Function as parameter
In the go language, functions also act as a data type, so functions can also be used as arguments to functions. For example:
Package Main
Import (
"FMT"
)
Func function (A, b int, sum func (int, int) int) {
Fmt. Println (sum (A, B))
}
Func sum (A, b int) int {
Return a + b
}
Func Main () {
var a, b int = 5, 6
F: = Sum
function (A, B, f)
}
Operation Result: One
The function sum acts as The formal parameter of the function, and the variable F is a function type () The argument at the time of the call.