Sum
Fixed
Create a function to get sum of three numbers.
func sum(a, b, c) {
return a + b + c
}
puts sum(3, 4, 5) // 12Dynamic
With array
func sum(nums) {
var sum = 0
for n : nums {
sum += n
}
return sum
}
puts sum([3, 4, 5]) // 12With variadic function
Last updated
Was this helpful?