Function Arguments in Golang - GeeksforGeeks

13 Aug, 2019
A function in Golang is a collection of statements which is used to perform specific task and return the result to the caller. A function can also perform some specific task without returning anything. Golang supports two different ways to pass arguments to the function i.e. Pass by Value or Call by Value and Pass By Reference or Call By Reference. By default, Golang uses the call by value way to pass the arguments to the function.
Basic Terms in Parameter Passing to Function:
The parameters passed to function are called Actual Parameters.
The parameters received by the function are called Formal Parameters.
In this parameter passing, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of the caller.
Example 1: In the below program, you can see that the value of Z cannot be modified by the function modify.
// Go program to illustrate the// concept of the call by valuepackage main  import "fmt"  // function which modifies// the valuefunc modify(Z int) {    Z = 70}  // Main functionfunc main() {      var Z int = 10          fmt.Printf("Before Function Call, value of Z is = %d", Z)      // call by value    modify(Z)          fmt.Printf("\nAfter Function Call, value of Z is = %d", Z)}
Output:
Before Function Call, value of Z is = 10
After Function Call, value of Z is = 10

Example 2: In the below program, swap function is unable to swap the values as we are using the call by value.
// Go program to illustrate the// concept of the call by valuepackage main  import "fmt"  // function which swap valuesfunc swap(x, y int) int {      // taking a temporary variable    var tmp int      tmp = x    x = y    y = tmp      return tmp}  // Main functionfunc main() {      var f int = 700    var s int = 900      fmt.Printf("Values Before Function Call\n")    fmt.Printf("f = %d and s = %d\n", f, s)      // call by values    swap(f, s)      fmt.Printf("\nValues After Function Call\n")    fmt.Printf("f = %d and s = %d", f, s)}
Output:
Values Before Function Call
f = 700 and s = 900

Values After Function Call
f = 700 and s = 900

Here, you will use the concept of Pointers. The dereference operator * is used to access the value at an address. The address operator & is used to get the address of a variable of any data type. Both the actual and formal parameters refer to the same locations, so any changes made inside the function are actually reflected in actual parameters of the caller.
Example 1: In the function call, we are passing the address of the variable and using the dereferencing operator * to modify the value. So after the function i.e. modify, you will find the updated value.
// Go program to illustrate the// concept of the call by Referencepackage main  import "fmt"  // function which modifies// the valuefunc modify(Z *int) {    *Z = 70}  // Main functionfunc main() {      var Z int = 10      fmt.Printf("Before Function Call, value of Z is = %d", Z)      // call by Reference    // by passing the address    // of the variable Z    modify(&Z)      fmt.Printf("\nAfter Function Call, value of Z is = %d", Z)}
Output:
Before Function Call, value of Z is = 10
After Function Call, value of Z is = 70

Example 2: By using the call by reference the swap function will be able to swap the values as shown below.
// Go program to illustrate the// concept of the call by Referencepackage main  import "fmt"  // function which swap values// taking the pointer to integerfunc swap(x, y *int) int {      // taking a temporary variable    var tmp int      tmp = *x    *x = *y    *y = tmp      return tmp}  // Main functionfunc main() {      var f int = 700    var s int = 900      fmt.Printf("Values Before Function Call\n")    fmt.Printf("f = %d and s = %d\n", f, s)      // call by Reference    // by passing the address     // of the variables    swap(&f, &s)      fmt.Printf("\nValues After Function Call\n")    fmt.Printf("f = %d and s = %d", f, s)}
Output:
Values Before Function Call
f = 700 and s = 900

Values After Function Call
f = 900 and s = 700

Go-Functions
Golang
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
6 Best Books to Learn Go Programming Language
Arrays in Go
strings.Replace() Function in Golang With Examples
How to Split a String in Golang?
Golang Maps
Slices in Golang
Different Ways to Find the Type of Variable in Golang
Inheritance in GoLang
Interfaces in Golang
How to Trim a String in Golang?