created variadic sum function
This commit is contained in:
parent
f4b408cf92
commit
5f11ccf9f6
18
headFirstCodeMagnets/variadic.go
Normal file
18
headFirstCodeMagnets/variadic.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func sum(numbers ...int) int {
|
||||||
|
var sum int = 0
|
||||||
|
|
||||||
|
for _, number := range numbers {
|
||||||
|
sum += number
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(sum(4, 1, 9, 2))
|
||||||
|
fmt.Println(sum(7))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user