I'm working through "The Swift Programming Language" and the associated playground file.
I have one section where I am asked: Write a function that calculates the average of its arguments.
Given the context, this is my solution
func averageOf(numbers: Int...) -> Int {
var sum = 0
var countOfNumbers = 0
for number in numbers {
sum += number
countOfNumbers += 1
}
var result: Double = Double(sum) / Double(countOfNumbers)
return result
}
averageOf()
averageOf(10, 20, 30)
As you can see I had to call the result as a Double (thats the sum / countOfNumbers).
However, I can't return result in this case because I get an error about converting return expression of type "Double".
So then I tried to return Double(result) without success.
Why does this not work and how can I best understand what I am doing wrong here?