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

Munir Niaz
Munir Niaz
1,831 Points

Help me to solve

Currently our greeting function only returns a single value. Modify it to return both the greeting and the language as a tuple. Make sure to name each item in the tuple: greeting and language. We will print them out in the next task.

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

    return greeting
}

2 Answers

You need to return a tuple. So you do this when declaring the function: -> (greeting: String, language: String) and then you return a tuple: return (greeting, language). Note that they asked you to name the members of the tuple.

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

    return (greeting, language)
}

let result = greeting("Tom")
println(result.language)
Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I would prefer to see what you've tried that's failed. That way we can help you with understanding the concept. In the definition of the function the thing after the arrow in the first line is what it's going to return. Right now, it only returns one string. And the actual return of the string is found at the bottom of the function. Here you return greeting. We need to return two strings and here's how we do that.

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

    return (greeting, language)
}

As you can see, in the definition we say we're going to return two strings. One named greeting and one named language. Inside the function we create those variables then assign them a string. At the end of the function we return both strings, just as we said we were going to do in the definition. Hope this helps!