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

Wang Lei
Wang Lei
2,766 Points

How to work on this code question?

I can't fully understand the instruction: pass the string "Tom" to the greeting function?

tuples.swift
func greetingFunc(#greeting: String, #language: String) -> (greeting: String, language: String) {
    let feedback = (greeting, language)
    return feedback
}
greetingFunc(greeting: "Hello", language: "English")

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hey there :) The assignment asks you to modify the existing function to return a tuple. That is, the input parameters don't need to be changed, only the return parameter. In this case, we want to return a named tuple, which, just as any function parameter, looks like (name: Type, name: Type) etc.

You can now return the greeting and the language constant via this tuple.

Moreover, the result, that is the tuple can be assigned to a variable as you can see below.

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

    return (greeting, language)
}

var result = greeting("Tom")

Hope that helps :)

P.S. You are working on a Swift 1 course which I consider deprecated, although you still can learn a lot of Swift here. However, with Swift 2 being around for a year now and Swift 3 being released with the new Xcode beta, I recommend doing the Swift 2 Track here on Treehouse.

Wang Lei
Wang Lei
2,766 Points

Problem solved. Thank you very much for your detailed explaining.

Wang Lei
Wang Lei
2,766 Points

Thank you so much!