[Go] Functions
Function can return multi values:
func printAge(age int) (int, int) {
return 12, age
}
func main() {
age1, age2 := printAge(8)
fmt.Println(age1)
fmt.Println(age2)
}
Function can have named return value: no need to tell what is the return value
func printAgeWithNamedRetrun() (ageOfSally int, ageOfBob int) {
ageOfSally = 12
ageOfBob = 8
return
}
func main() {
fmt.Println(printAgeWithNamedRetrun()) // 12, 8
}
Variadic Function:
func average(num1, num2, num3 float64) float64 {
return (num1 + num2 + num3) / 3
}
func average2(nums ...float64) float64 {
total := 0.0
for _, number := range nums {
total += number
}
return total / float64(len(nums))
}
func main() {
fmt.Println(average(2.2, 3.3, 4.4))
fmt.Println(average2(2.2, 3.3, 4.4))
}