Golang 函数参数

Golang 函数参数

Golang中的函数是一个语句的集合,用于执行特定的任务并将结果返回给调用者。一个函数也可以执行一些特定的任务而不返回任何东西。Golang支持两种不同的方式来传递参数给函数,即通过值传递或通过值调用,以及通过引用传递或通过引用调用。默认情况下,Golang使用按值调用的方式来传递参数给函数。

向函数传递参数的基本术语

  • 传递给函数的参数被称为实际参数。
  • 函数收到的参数被称为正式参数。

按值调用

在这种参数传递中,实际参数的值被复制到函数的形式参数中,这两类参数被存储在不同的内存位置。因此,在函数内部所做的任何改变都不会反映在调用者的实际参数中。

例1: 在下面的程序中,你可以看到Z的值不能被函数modify .NET修改。

// Go program to illustrate the
// concept of the call by value
package main
  
import "fmt"
  
// function which modifies
// the value
func modify(Z int) {
    Z = 70
}
  
// Main function
func main() {
  
    var Z int = 10
      
    fmt.Printf("Before Function Call, value of Z is = %d", Z)
  
    // call by value
    modify(Z)
      
    fmt.Printf("\nAfter Function Call, value of Z is = %d", Z)
}

输出

Before Function Call, value of Z is = 10
After Function Call, value of Z is = 10

例2: 在下面的程序中,swap函数无法交换数值,因为我们使用的是按值调用。

// Go program to illustrate the
// concept of the call by value
package main
  
import "fmt"
  
// function which swap values
func swap(x, y int) int {
  
    // taking a temporary variable
    var tmp int
  
    tmp = x
    x = y
    y = tmp
  
    return tmp
}
  
// Main function
func main() {
  
    var f int = 700
    var s int = 900
  
    fmt.Printf("Values Before Function Call\n")
    fmt.Printf("f = %d and s = %d\n", f, s)
  
    // call by values
    swap(f, s)
  
    fmt.Printf("\nValues After Function Call\n")
    fmt.Printf("f = %d and s = %d", f, s)
}

输出

Values Before Function Call
f = 700 and s = 900

Values After Function Call
f = 700 and s = 900

通过引用调用

在这里,你将使用指针的概念。解除引用操作符*用于访问一个地址的值。地址操作符&用于获取任何数据类型的变量的地址。实际参数和形式参数都指向相同的位置,所以在函数内部的任何改变实际上都反映在调用者的实际参数中。

例1: 在函数调用中,我们传递了变量的地址,并使用去引用操作符*来修改该值。因此,在函数即修改之后,你会发现更新的值。

// Go program to illustrate the
// concept of the call by Reference
package main
  
import "fmt"
  
// function which modifies
// the value
func modify(Z *int) {
    *Z = 70
}
  
// Main function
func main() {
  
    var Z int = 10
  
    fmt.Printf("Before Function Call, value of Z is = %d", Z)
  
    // call by Reference
    // by passing the address
    // of the variable Z
    modify(&Z)
  
    fmt.Printf("\nAfter Function Call, value of Z is = %d", Z)
}

输出

Before Function Call, value of Z is = 10
After Function Call, value of Z is = 70

例2: 通过使用引用调用,交换函数将能够交换值,如下所示。

// Go program to illustrate the
// concept of the call by Reference
package main
  
import "fmt"
  
// function which swap values
// taking the pointer to integer
func swap(x, y *int) int {
  
    // taking a temporary variable
    var tmp int
  
    tmp = *x
    *x = *y
    *y = tmp
  
    return tmp
}
  
// Main function
func main() {
  
    var f int = 700
    var s int = 900
  
    fmt.Printf("Values Before Function Call\n")
    fmt.Printf("f = %d and s = %d\n", f, s)
  
    // call by Reference
    // by passing the address 
    // of the variables
    swap(&f, &s)
  
    fmt.Printf("\nValues After Function Call\n")
    fmt.Printf("f = %d and s = %d", f, s)
}

输出

Values Before Function Call
f = 700 and s = 900

Values After Function Call
f = 900 and s = 700

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程