iOS Functions in Swift Functions in Swift Recap: Functions

Alex Deltomme
Alex Deltomme
1,628 Points

Need help for Challenge Task 2/3 (Recap: Functions)

This is probably completely wrong, but could someone correct this and give me a hint on how to complete it? Not the solution, just a hint would be nice.

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

3 Answers

Dan Lindsay
Dan Lindsay
39,611 Points

Hey Alex,

I don’t really know how to help more without giving the answer, so I will just try to give a good explanation of the answer.

I assume you got the first task done, and if not, it would look like this:

func temperatureInFahrenheit(temperature: Double) -> Double {
  return temperature
}

Let’s take the rest one step at a time. In the second part of the challenge, they ask us to get rid of the existing return statement. So now our function looks like this:

func temperatureInFahrenheit(temperature: Double) -> Double {

}

It is implied that the temperature parameter of our function is going to be in Celcius, as we don’t convert Fahrenheit to Fahrenheit, and there is no mention of Kelvin(the only other measurement of temperature that I know of). Knowing that, we will not need to create any other variables inside our function. All we need to do is return temperature converted to Fahrenheit, like so:

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

and this part of the challenge is complete. Sorry I had to give the answer, but I couldn’t think of another hint for you. Still, I hope this explanation helps you out.

Dan

Alex Deltomme
Alex Deltomme
1,628 Points

Now I understand, thanks a lot!

Dan Lindsay
Dan Lindsay
39,611 Points

Hi Alex,

A couple of hints for you. First, your function has a parameter named temperature, which you are not using. Also, do you need to create any other variables to solve this challenge, or can it be done without any extra variables?

Hope this helps!

Dan

Alex Deltomme
Alex Deltomme
1,628 Points

I'm still pretty unsure on what to do... :/

Alex Abid
PRO
Alex Abid
Pro Student 810 Points

Hi Dan, quick question on your answer. In the problem itself it says to "return fahrenheit" and convert celsius.

I'm thankful for the correct answer but why is it that we don't need to actually use those titles within the code at all?