Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift 2.0 Functions Functions in Swift 2.0 Recap: Functions

What is wrong with this code?

func temperatureInFahrenheit(temperature: Double)-> Double {
result = (temperature * 9) / 5
result = result + 32
return result
}

2 Answers

Hi there,

In your code, result isn't declared. Add the word var before its first usage - that will fix your code.

func temperatureInFahrenheit (temperature: Double) -> Double {
  var result = (temperature * 9) / 5
  result = result + 32
  return result
}

Alternatively, try a solution on one line like this:

func temperatureInFahrenheit(temperature: Double) -> Double {
  return ((temperature * 9) / 5) + 32
}

I hope that helps.

Steve.

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Abdijabar Mohamed,

The code challenge asks us to create a function that converts a temperature from Celsius to Fahrenheit. The problem with your code is that you're not declaring result as a variable or a constant. To get your code to pass, you need to declare result as a variable.

func temperatureInFahrenheit(temperature: Double) -> Double {
    var result = (temperature * 9) / 5
    result = result + 32
    return result
}

You can condense the operations of the function to be on a single line, because of the order of operations. Here's the solution I recommend:

func temperatureInFahrenheit(temperature: Double) -> Double {
    let result = (temperature * 9)/5 + 32
    return result
}

Good Luck!

Thanks a heap

Thanks a heap