Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift 2.0 Functions Functions in Swift 2.0 Recap: Functions

SONU PRABHAKAR
SONU PRABHAKAR
2,039 Points

Kindly help me to solve this temperature conversion function...

i have written question in snippet below.

functions.swift
// Enter your code below
func temperatureInFahrenheit (temperature: Double) -> Double{

temperatureInFarenheit = (temperature * 9)/5 + 32

return(temperature)
}
//question is given below:
/*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.*/
Jose Patarroyo
Jose Patarroyo
2,953 Points

As you should have guess by now, the problem is that you are calling the function within itself, it doesn't work that way. The way to go is to declare a constant that will store the temperature in fahrenheit with the formula you have. Once it has the value stored, you should return that value. Hopefully what I just said makes sense to you.

4 Answers

Jose Patarroyo
Jose Patarroyo
2,953 Points

As you should have guess by now, the problem is that you are calling the function within itself, it doesn't work that way. The way to go is to declare a constant that will store the temperature in fahrenheit with the formula you have. Once it has the value stored, you should return that value. Hopefully what I just said makes sense to you.

Justin Bonaccorso
Justin Bonaccorso
2,781 Points
func temperatureInFahrenheit(temperature: Double) -> Double{
   let celcius = (temperature * 9 / 5 + 32)
    return celcius

}

It does get a bit confusing, but understanding that I needed to store the value for celcius and then return that same value, it clicked a bit better for me. Hopefully that helps.

Digvijay Jaiswal
Digvijay Jaiswal
5,565 Points

Hi Justing,

your answer is correct, all i would add is we are calculating Fahrenheit here so rather than using celcius = blah blah we should use fahrenheit = blah blah. like below.

func temperatureInFahrenheit (temperature: Double) -> Double {

let fahrenheit = ((temperature * 9)/5) + 32

return fahrenheit

Tyron Spencer
Tyron Spencer
1,489 Points

Hi Digvijay Jaiswal Thankyou this has helped me also :)