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

Declare a function named temperatureInFahrenheit. The function should take a single input named temperature of type Doub

Declare a function named temperatureInFahrenheit. The function should take a single input named temperature of type Double and has a return type of Double.

For now, to make the function pass, simply return the value of the temperature parameter from the function. In the next task, we'll add more code to the body.

I keep getting an error of unexpected non void return value

// Enter your code below

1 Answer

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Hey Allison,

Without seeing your code, I'm not able to explain why you are receiving that error.

The challenge wants a function declared with one parameter called "temperature" that will take the type of double. Then you have to declare the return type (using the -> symbol) ... also of type double. Then inside the code block just return the parameter passed into the function.

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

I hope that helps. :dizzy:

Dear Jason Anders, why can't I just :

func temperatureInFahrenheit(temperature: Double) {
  return temperature
Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Hi Wei Wei Shen

I don't have a technical answer for you, so I will give what I believe to be a simple answer. Swift likes to be told what type the function is going to return. Swift is more or less build off of Objective-C, where you always specify type. With that, that's why the instructions for the challenge included what type needed to be returned.

See, you could have the function take in a Double as this one does, but then have it return an Int (after some code inside of the function). So really, Swift just wants to know what to expect.