2.11. Go language function values pass values

发布时间 : 2025-10-25 13:32:59 UTC      

Page Views: 10 views

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.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。