In Go, a function is a block of code that performs a specific task and returns a result. Functions can take zero or more input parameters and can return zero or more output values.
Here is an example of a simple function that takes two integers as input parameters and returns their sum:
package main
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
func main() {
fmt.Println(split(17)) // "4 13"
}
package main
import "fmt"
func add(args ...int) int {
total := 0
for _, v := range args {
total += v
}
return total
}
func main() {
fmt.Println(add(1, 2, 3)) // 6
fmt.Println(add(1, 2, 3, 4)) // 10
}