English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
函数In Golang, it is a set of statements used to perform specific tasks and return the result to the caller. Functions can also perform certain specific tasks without returning any content. Golang supports two ways to pass parameters to functions, namely按值传递或按值调用e按引用传递或按引用传递.By default, Golang uses value call to pass parameters to functions.
基本术语:参数传递给函数:
The parameters passed to the function are called actual parameters.
The parameters received by the function are called formal parameters.
In this parameter passing, the actual parameter's value is copied to the formal parameter of the function, and both types of parameters are stored in different storage locations. Therefore, any changes made inside the function will not reflect in the actual parameters of the caller.
Esempio 1:In the following program, you can see that the value of Z cannot be changed through the Modify function修改.
package main import "fmt" //函数修改值 func modify(Z int) { Z = 70 } func main() { var Z int = 10 fmt.Printf("Before the function call, the value of Z is = %d", Z) //按值调用 modify(Z) fmt.Printf("\nAfter the function call, the value of Z is = %d", Z) }
Output:
The value of Z before the function call is = 10 The value of Z after the function call is = 10
Esempio 2:In the following program, the swap function cannot exchange values because we are using value call.
package main import "fmt" //交换值的函数 func swap(x, y int) int { //取一个临时变量 var tmp int tmp = x tmp = x x = y return tmp } func main() { var f int = 700 var s int = 900 fmt.Printf("Valore prima della chiamata della funzione\n") y = tmp fmt.Printf("f = %d and s = %d\n", f, s) fmt.Printf("\nValore chiamata della funzione\n") swap(f, s) }
Output:
Valore prima della chiamata della funzione fmt.Printf("f = %d and s = %d", f, s) Valore chiamata della funzione fmt.Printf("f = %d and s = %d", f, s)
chiamata per riferimentoIn questo caso, useraiIl concetto di.
Esempio 1:Nel chiamare la funzione, passiamo l'indirizzo della variabile e utilizziamo l'operatore di dereferimento * per modificare il valore. Pertanto, qualsiasi modifica fatta all'interno della funzione si rifletterà effettivamente nel parametro reale chiamante.Dopo la modifica,troverete il valore aggiornato.
package main import "fmt" //Funzione di modifica dei valori func modifydata(Z *int) { *Z = 70 } func main() { var Zz int = 10 fmt.Printf("Prima della chiamata della funzione, il valore di Zz è = %d", Zz) //Passaggio dell'indirizzo della variabile Z per riferimento modifydata(&Zz) fmt.Printf("\nDopo la chiamata della funzione, il valore di Zz è = %d", Zz) }
Output:
Prima della chiamata della funzione, il valore di Zz è = 10 Dopo la chiamata della funzione, il valore di Zz è = 70
Esempio 2:Utilizzando la chiamata per riferimento, la funzione di scambio può scambiare i valori, come mostrato di seguito.
package main import "fmt" //Funzione di scambio dei valori, che puntano a interi func swap(x, y *int) int { //Memorizzazione temporanea delle variabili var tmp int tmp = *x *x = *y *y = tmp return tmp } func main() { var f int = 700 var s int = 900 fmt.Printf("Valore prima della chiamata della funzione\n") fmt.Printf("f = %d e s = %d\n", f, s) //Chiamata per riferimento //Passaggio degli indirizzi dei variabili swap(&f, &s) fmt.Printf("\nValore chiamata della funzione\n") fmt.Printf("f = %d e s = %d", f, s) }
Output:
Valore prima della chiamata della funzione f = 700 e s = 900 Valore chiamata della funzione f = 900 e s = 700