函数可以使用某些参数来接收值并相应地执行所需的任务。带有两个参数的函数的语法是:
func function_name( [parameter_1 parameter_2] ) [return_types]
{
body of the function
}
示例 1:没有任何返回类型
// Golang program that uses func with
// two arguments without any return type
package main
import "fmt"
// Function accepting two arguments
// arguments defined as int type
func add(x int, y int) {
var sum int = x + y
fmt.Println(sum) // Prints the sum
}
// main function
func main() {
add(10, 20) // Passing arguments
}
输出:
30
示例 2:带返回类型
// Golang program that uses func with
// two arguments with any return type
package main
import "fmt"
// Function accepting two arguments
// arguments and return defined
// as int type
func add(x int, y int) int {
var z int = x + y
return z
}
// main function
func main() {
// Passing arguments and receiving
// the returned value in sum
var sum = add(10, 20)
// Prints the sum
fmt.Println(sum)
}
输出:
30