2.11. Go language function values pass values

发布时间 :2023-10-12 23:00:08 UTC      

Passing means that a copy of the actual parameter is passed to the function when the function is called, so that if the parameter is modified in the function, the actual parameter will not be affected.

By default Go language uses value passing, that is, the actual parameters are not affected during the call.

The following definitions swap() function:

/* Define functions that exchange values with each other */
func swap(x, y int) int {
   var temp int

   temp = x /* Save the value of x */
   x = y    /* Assign y value to x */
   y = temp /* Assign the temp value to y*/

   return temp;
}

Next, let’s use value passing to call the swap() function:

package main

import "fmt"

func main() {
   /* Define local variables */
   var a int = 100
   var b int = 200

   fmt.Printf("The value of a before exchange is : %d\n", a )
   fmt.Printf("The value of b before exchange is : %d\n", b )

   /* Exchange values by calling functions */
   swap(a, b)

   fmt.Printf("The value of a after exchange: %d\n", a )
   fmt.Printf("The value of b after exchange: %d\n", b )
}

/* Define functions that exchange values with each other */
func swap(x, y int) int {
   var temp int

   temp = x /* Save the value of x */
   x = y    /* Assign y value to x */
   y = temp /* Assign the temp value to y*/

   return temp;
}

The following code execution results are:

The value of a before exchange is: 100
The value of b before exchange is: 200
Value of a after exchange: 100
Value of b after exchange: 200

Value passing is used in the program, so the two values do not interact with each other, and we can use reference passing to achieve the exchange effect.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.