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)    // 12

Dynamic

With array

func sum(nums) {
    var sum = 0
    for n : nums {
        sum += n
    }
    return sum
}

puts sum([3, 4, 5])    // 12

With variadic function

Last updated

Was this helpful?