Functions - Variadic Parameters - Question

Hello,

I have gone through this challenge and wanted to convert the final result from seconds to minutes. I am still getting an error message—could anyone shed light onto what part I’m not seeing? Thanks!

Code:

func avgSongLength(times: Int...) -> Int {
  var total = 0
  for time in times {
    total += time
  }

  var convertedLength: Int = (total / times.count) / 60)
  return "The average length of all 10 Eurovision songs is \(convertedLength) minutes."
}

print(avgSongLength(times: 183, 176, 180, 176, 184, 179, 181, 180, 172, 178))

Errors:

/home/ccuser/workspace/swift-functions-variadic-parameters/Sources/workspace/main.swift:9:56: error: consecutive statements on a line must be separated by ‘;’
var convertedLength: Int = (total / times.count) / 60)
^
;
/home/ccuser/workspace/swift-functions-variadic-parameters/Sources/workspace/main.swift:9:56: error: expected expression
var convertedLength: Int = (total / times.count) / 60)
^
/home/ccuser/workspace/swift-functions-variadic-parameters/Sources/workspace/main.swift:10:10: error: cannot convert return expression of type ‘String’ to return type ‘Int’
return “The average length of all 10 Eurovision songs is (convertedLength) minutes.”
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  1. Variadic Parameters
    11/13

The main issue is that your function is declared to return an Int when in fact it seems you want to return a String.
You may also want to change var convertedLength to let convertedLength as the compiler seems to like this more.

If you change your declaration it runs in either case!

1 Like

In addition to @toastedpitabread’s observation that your function is declared to return an Int rather than the String in your return statement, I think the error you are receiving is due to a missing parenthesis in this line:

var convertedLength: Int = (total / times.count) / 60)
1 Like