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 Functions in Swift Functions in Swift Recap: Functions

I need help setting this function up correctly

Can someone please explain what i'm missing or doing wrong and how to set it up

functions.swift
// Enter your code below
func temperatureInFahrenheit(temperature: Double, Celcius: Double) -> Double {
  let Celcius = temperatureInFahrenheit * 9 / 5 + 32

  return temperatureInFahrenheit
   }

These were the instructions: Let's add some code to the body of the function. Get rid of the existing return statement that's in there and in this task we're going to write out code to take a Celcius temperature value passed in and convert it to Fahrenheit.

To convert from Celcius to Fahrenheit - multiply the value by 9, divide the resulting value by 5 and then add 32.

Once you have the Fahrenheit value, return it from the function.

2 Answers

Rogier Nitschelm
seal-mask
.a{fill-rule:evenodd;}techdegree
Rogier Nitschelm
iOS Development Techdegree Student 5,461 Points

Some tips:

  • your function should only have 1 parameter (as you can calculate fahrenheit based on the temperature in celsius);
  • remember to calculate the temperature based on the argument that is passed in (as currently you are making a calculation based on the function name, which will not get you a desired result).
  • wouldn't fahrenheit be a better variable name?
  • also remember to return the value you calculate, not the function itself. So if you store the result in a variable named fahrenheit, you can then return fahrenheit. (also, you could skip variable declaration and just return the calculation itself).
Jeff McDivitt
Jeff McDivitt
23,970 Points

Rogier did a great job of explaining what needs to be done; here is the correct code

func temperatureInFahrenheit(temperature: Double) -> Double {

  return temperature * 9 / 5 + 32

}