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 question driving me crazy because it dont make any sense?

http://teamtreehouse.com/library/functions-and-optionals/functions/function-return-types-2

Modify the definition of the function named greeting to return a String and replace the println statement with a return statement. The function should return a greeting. For example, if you pass in a string "Tom" then the return string would be: "Hello Tom".

func greeting(person: String) -> String {
    let name = person
    return name
}

greeting("Tom")

idid both none work

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

greeting("tom")

println("my name is \(greeting("tom"))")

6 Answers

Chris Jones
STAFF
Chris Jones
Treehouse Guest Teacher

If you break this question down into steps, it's very simple.

Based on the code challenge linked to, this is the code you start out with.

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

Step one of the question: Modify the definition of the function named greeting to return a String

This is pretty straight forward. The function is currently returning nothing and we need to tell it to return a String.

//Before
func greeting(person: String) {

//After
func greeting(person: String) -> String {

Step two of the question: and replace the println statement with a return statement.

All you have to do here is remove println() and replace it with return. That's it.

//Before
    println("Hello \(person)")

//After
    return "Hello \(person)"

The rest of the question just gives a little more detail. Now, if you give your completed function a call like so greeting("Tom"), your return string should be "Hello Tom".

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

//Completed
func greeting(person: String) -> String {
    return "Hello \(person)"
}
Chris Jones
STAFF
Chris Jones
Treehouse Guest Teacher

I think you're missing the minor detail here. Below is the original code from the question.

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

You're almost all the way there, however, the instructions state that you should return a greeting by replacing the println statement with a return statement. Right now, you're simply returning the string that was passed into the function (assumed to be a persons name), not a complete greeting. I'm trying not to just give you the answer and I think you can figure it out from here.

Simon Jensen
Simon Jensen
3,558 Points

The questing was hard for me to understand too. Maybe the text should be changed and be a little bit easy'er to understand

remember to give the func a -> String

Doesn't make sense for me either. Somebody please rephrase the question so it would make sense, given the context of the video and the example that was provided.

Right now, we can't connect what was taught with what is being asked in the challenge. There needs to be a logical connection so it'll make sense for the students.

Thanks Treehouse

thank u