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 Functions and Optionals Functions Function Return Types

Return Statement with Functions in iOS

I'm lost! It's asking me to return "Hello Tom" instead of printing it. I think that the -> confuses me beyond belief can anyone help me out?

returns.swift
func greeting(person: String) -> (greetings: String, person: String) {
    person = "Tom"
    return("Hello \(person)")
}

2 Answers

Hi Darin,

Let's break this down.

You start with this code:

func greeting(person: String) {
    println("Hello \(person)")
}

And this task:

Modify the definition of the function named greeting to return a String and replace the println statement with a return statement.

So, you've been given a function that takes one parameter that then prints something to the screen. The challenge wants you to return the output rather than print it out.

First, you have to stipulate that the function returns something. You've correctly identified that it needs the arrow thingy to show that. However, all you are returning is the result of "Hello \(person)" which is a String.

So, the func starts with func greeting(person: String) -> String{}.

The function still takes a String parameter as before, but now returns a String too, due to the arrow thingy. I'm sure it has a complicated name, but like arrow thingy.

Within the function, you need to change the println so that the func returns the String result, rather than prints it.

The end result will look like:

func greeting(person: String) -> String {
    return("Hello \(person)")
}

I hope that makes sense!

Steve.

Thanks Steve!

made it easier to understand!

Now I can move on!

No problem - glad I could help.

Steve.