Go - Call by reference

The name with the aid of reference method of passing arguments to a function copies the cope with of a controversy into the formal parameter.

you need to claim the function parameters as pointer types as in the following function change(), which exchanges the values of the 2 integer variables pointed to by its arguments.

/* feature definition to switch the values */
func switch(x *int, y *int) 
   var temp int
   temp = *x    /* keep the price at cope with x */
   *x = *y      /* positioned y into x */
   *y = temp    /* placed temp into y */

For now, let us call the feature switch() through passing values by means of reference as inside the following example

package foremost

import "fmt"

func fundamental() 
   /* neighborhood variable definition */
   var a int = 350
   var b int = 500

   fmt.Printf("Before swap, fee of a : %dn", a )
   fmt.Printf("Before switch, fee of b : %dn", b )

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

   fmt.Printf("After swap, price of a : %dn", a )
   fmt.Printf("After change, cost of b : %dn", b )

func switch(x, y int) int 
   var temp int

   temp = x /* shop the value of x */
   x = y    /* positioned y into x */
   y = temp /* positioned temp into y */

   return temp;

Put the above code in a single C file, after which compile and execute it. It produces the following result
$cross run fundamental.Cross
Before change, fee of a : 350
Before switch, cost of b : 500
After swap, price of a : 350
After switch, fee of b : 500

It suggests that the change has meditated out of doors the feature as properly, not like name via cost wherein the adjustments do now not replicate outside the characteristic.