Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vibinnair/a3bd9f7aa714095f34020803632f0811 to your computer and use it in GitHub Desktop.
Save vibinnair/a3bd9f7aa714095f34020803632f0811 to your computer and use it in GitHub Desktop.
import Foundation
func sumOfElements(_ array: [Int], _ sum: Int) -> Int {
if array.count <= 0 {
return sum
}
let newArray = Array(array.dropFirst(1))
return sumOfElements(newArray, sum + array.first!)
}
print("Sum of elements in array is: \(sumOfElements([10, 20, 30], 0))")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment