SoFunction
Updated on 2025-03-04

Basic methods for passing and calling parameters of functions in Go language

Passing function parameters by value is a method call that copies the actual value of the parameter to the formal parameters of the function. In this case, the change of parameters within the function will not affect the parameters.

By default, the Go programming language uses calls to pass parameters through values. In general, this means that the inside of the function code cannot change the parameters used to call the function. Consider the definition of the function swap() as follows.

Copy the codeThe code is as follows:

/* 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:
Copy the codeThe code is as follows:

 package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 100
   var b int = 200

   ("Before swap, value of a : %d\n", a )
   ("Before swap, value of b : %d\n", b )

   /* calling a function to swap the values */
   swap(a, b)

   ("After swap, value of a : %d\n", a )
   ("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 a 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 been changed, although they have changed inside the function.

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

To pass a value by reference, the pointer of the parameter is passed to the function just like any other value. Therefore, accordingly, it is necessary to declare that the parameter of the function is pointer type, such as the function swap() below, and its exchange values ​​of two integer variables point to its parameters.

Copy the codeThe code is as follows:

/* 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() function by reference as passing the numeric value in the following example:
Copy the codeThe code is as follows:

package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 100
   var b int= 200

   ("Before swap, value of a : %d\n", a )
   ("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)

   ("After swap, value of a : %d\n", a )
   ("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 a 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 function of the change and the changes that are different from those invoked by the value cannot reflect outside the function.