The basic method for passing and calling arguments to functions in the Go language

  • 2020-05-30 20:20:34
  • OfStack

Passing function parameters by value is a method call that copies the actual value of the parameter to the formal parameter of the function. In this case, parameter changes within the function have no effect on the parameter.

By default, the Go programming language USES calls to pass parameters by value. In the case of rule 1, this means that the parameters used to call the function cannot be changed within the function. Consider the function swap() as defined below.


/* function definition to swap the values */
func swap(int x, int y) int {
   var temp int    temp = x /* save the value of x */
   x = y    /* put y into x */
   y = temp /* put temp into y */    return temp;
}

Now, let's call the function swap() by making the actual value as in the following example:

 package main import "fmt" func main() {
   /* local variable definition */
   var a int = 100
   var b int = 200    fmt.Printf("Before swap, value of a : %d\n", a )
   fmt.Printf("Before swap, value of b : %d\n", b )    /* calling a function to swap the values */
   swap(a, b)    fmt.Printf("After swap, value of a : %d\n", a )
   fmt.Printf("After swap, value of b : %d\n", b )
}
func swap(x, y int) int {
   var temp int    temp = x /* save the value of x */
   x = y    /* put y into x */
   y = temp /* put temp into y */    return temp;
}

Let's put the above code in an C file, compile and execute it, and it will produce the following results:


Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200

This indicates that the parameter values have not changed, although they have changed within the function.

By passing the function parameter, that is, the address of the copy parameter to the reference method call of the formal parameter. Inside the function, the address is the actual parameter used in the access call. This means that changes to parameters affect the parameters passed.

To pass a value by reference, a pointer to the argument is passed to the function just like any other value. Accordingly, you need to declare that the parameter of the function is of pointer type as follows: swap(), whose values of the swapped two integer variables point to its parameter.


/* function definition to swap the values */
func swap(x *int, y *int) {
   var temp int
   temp = *x    /* save the value at address x */
   *x = *y      /* put y into x */
   *y = temp    /* put temp into y */
}

Now, let's call the function swap() to pass the value by reference as in the following example:

package main import "fmt" func main() {
   /* local variable definition */
   var a int = 100
   var b int= 200    fmt.Printf("Before swap, value of a : %d\n", a )
   fmt.Printf("Before swap, value of b : %d\n", b )    /* calling a function to swap the values.
   * &a indicates pointer to a ie. address of variable a and
   * &b indicates pointer to b ie. address of variable b.
   */
   swap(&a, &b)    fmt.Printf("After swap, value of a : %d\n", a )
   fmt.Printf("After swap, value of b : %d\n", b )
} func swap(x *int, y *int) {
   var temp int
   temp = *x    /* save the value at address x */
   *x = *y    /* put y into x */
   *y = temp    /* put temp into y */
}

Let's put the above code in an C file, compile and execute it, and it will produce the following results:


Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

This indicates that the functionality of the changes and changes that are different from external representations that are called through a value cannot be reflected outside the function.


Related articles: