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 Parameters and Tuples Tuples

Safeer Abubacker
Safeer Abubacker
1,209 Points

Create a variable named result and assign it the tuple returned from function greeting. I tried so many ways but not get

Create a variable named result and assign it the tuple returned from function greeting. (Note: pass the string "Tom" to the greeting function.).. I am trying to solve this task but not able to get. Any help ?

2 Answers

func greeting(person: String) -> (language: String, greeting: String) {
    let language = "English"
    let greeting = "Hello \(person)"

    return (language, greeting)
}

var result = greeting("Tom")
println("\(result.language)")

You need to pass the greeting function the name "Tom". That's the string that the function parameter takes--this parameter is named "person" within the context of the function itself, and it accepts the type String (i.e. person: String). If you're getting confused on the naming, remember that what is returned by the function refers to the named tuple return types (i.e. (language: String, greeting: String)) in the first line.

Once you pass it "Tom", the function returns the tuple and assigns it to the result variable you create.

Hey Safeer,

I answered this one yesterday here; it's an ambiguous question — not your fault. Basically, the tuple you return has to be a certain order — specifically, this order:

return (greeting, language)