"Swift Basics" was retired on May 31, 2020.

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

s t
s t
949 Points

SOS: help with IOS Swift functions test please

Hello all,

I have the following task (step 2) that I'm stuck on:

""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.""

I don't understand why the let Fahrenheit within the function should not be there... do I not have to make a variable or constant with in the function to identify the calculation I want to take place? although my question is conceptual I'm pretty sure what I have attempted to write for this function is completely wrong so a point in the right direction would be greatly appreciated.

I look forward to hearing from whomever can help,

Thanks again,

Kind regards ST

functions.swift
var celcius = 10

func temperatureInFahrenheit(temperature: Double) -> Double  {
    let fahrenheit = celcius * 9 % 5 + 32


}

2 Answers

3 things:

  1. You used the modulo(%) operator instead of the division(/) operator. Modulo gives you the remainder.
  2. Since we are just creating a reusable function you do not need to create a variable named celsius.
  3. Also don't forget to return the value.
// Enter your code below
func temperatureInFahrenheit(temperature: Double) -> Double {
  let fahrenheit = temperature * 9 / 5 + 32

  return fahrenheit
}

If this helped you please remember to up vote and mark it as best answer. Thanks!

s t
s t
949 Points

Once again Brandon, Thank you for your time and help!!