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

Jericoe States
Jericoe States
3,262 Points

Recap: Function 3/3

func temperatureInFahrenheit(temperature: Double) -> Double {

return(temperature * 9) / 5 + 32

}

temperatureInFahrenheit(temperature: 24.0)

let temperatureInFahrenheit(temperature: Double) = fahrenheitTemp

//call the function and pass in a value of 24.0 degrees. Assign the result of the function to a

constant named fahrenheitTemp.

//Why does this code not take? I feel its the simplest thing but I just can't get it.

functions.swift
func temperatureInFahrenheit(temperature: Double) -> Double {


    return(temperature * 9) / 5 + 32

}

temperatureInFahrenheit(temperature: 24.0)

let farenheitTemp = temperatureInFahrenheit(temperature: <#T##Double#>)

2 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Not sure why you are including the last part of your code

func temperatureInFahrenheit(temperature: Double) -> Double {

return temperature * 9 / 5 + 32

}

let fahrenheitTemp = temperatureInFahrenheit(temperature: 24.0)
Jericoe States
Jericoe States
3,262 Points

ye I figured it out right after I sent my message, thank you

Jenny Dogan
Jenny Dogan
4,595 Points

Hey Jeff,

I followed the video and wrote my code a different way and was still able to pass. Can you help answer the following question?

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

After looking at your code, I noticed I created an extra constant "fahrenheit", if I did that does that mean the last statement is passing in the value of constant "fahrenheit" into "fahrenheitTemp" where yours is just passing in the result of the calculation into "fahrenheitTemp"? Thanks!

Jeff McDivitt
Jeff McDivitt
23,970 Points

Yes you are correct you basically created a new variable which is ok but not needed

See some Apple documentation

Here they created the variable greeting

func greet(person: String) -> String {
    let greeting = "Hello, " + person + "!"
    return greeting
}

but it is really not needed because you can just combine and use return

func greetAgain(person: String) -> String {
    return "Hello again, " + person + "!"
}

Let me know if that makes sense