English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
FunctionIn Golang, a function is a set of statements used to execute specific tasks and return the result to the caller. Functions can also execute certain specific tasks without returning any content. Golang supports two ways to pass parameters to functions, namelyBy value passing or by value callingandBy reference passing or by reference passing.By default, Golang uses value passing to pass parameters to functions.
Basic terms for parameter passing to a function:
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 within the function will not be reflected in the actual parameter of the caller.
Example1:In the following program, you can see that the value of Z cannot be modified by the Modify functionModification.
package main import "fmt" // Function to modify the value 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) //By value call modify(Z) fmt.Printf("\nAfter the function call, the value of Z is = %d", Z) }
Output:
Before the function call, the value of Z is = 10 After the function call, the value of Z is = 10
Example2:In the following program, the swap function cannot swap values because we are using value calling.
package main import "fmt" //A function to swap values func swap(x, y int) int { //Take a temporary variable var tmp int tmp = x x = y y = tmp return tmp } func main() { var f int = 700 var s int = 900 fmt.Printf("The value before the function call\n") fmt.Printf("f = %d and s = %d\n", f, s) swap(f, s) fmt.Printf("\nThe value after the function call\n") fmt.Printf("f = %d and s = %d", f, s) }
Output:
The value before the function call f = 700 and s = 900 The value after the function call f = 700 and s = 900
Here, you will usePointers (Pointers)concept. The dereference operator*to access the value in the address. The address operator & is used to get the address of any data type variable. Actual parameters and formal parameters both point to the same location, so any changes made inside the function will actually reflect in the actual parameters of the caller.
Example1:In the function call, we pass the address of the variable and use the dereference operator*Modify the value. Therefore, in the function asAfter ModifyYou will find the updated value.
package main import "fmt" // A function to modify values func modifydata(Z *int) { *Z = 70 } func main() { var Zz int = 10 fmt.Printf("Before the function call, the value of Zz is = %d", Zz) //Passing the address of variable Z by reference modifydata(&Zz) fmt.Printf("\nAfter the function call, the value of Zz is = %d", Zz) }
Output:
Before the function call, the value of Zz is = 10 After the function call, the value of Zz is = 70
Example2:By using reference calling, the swap function will be able to swap values as shown below.
package main import "fmt" //A function to swap values, pointing to integer func swap(x, y *int) int { //Temporary storage variable var tmp int tmp = *x *x = *y *y = tmp return tmp } func main() { var f int = 700 var s int = 900 fmt.Printf("The value before the function call\n") fmt.Printf("f = %d and s = %d\n", f, s) //By reference //Passing the address of the variable swap(&f, &s) fmt.Printf("\nThe value after the function call\n") fmt.Printf("f = %d and s = %d", f, s) }
Output:
The value before the function call f = 700 and s = 900 The value after the function call f = 900 and s = 700