Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Parameters and Tuples Tuples

Jordan Nilsson
Jordan Nilsson
1,409 Points

What is a tuple?

I don't understand what a tuple is. I don't understand what I'm supposed to do to complete the first task of this challenge.

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

    return blah
}

1 Answer

Hello Jordan Nilsson

Here is an example of the code you provide without a tuple, and then I will add the tuple.

So here we have the code you provided, please notice the comments in green.

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

    return blah // Where is blah coming from?
}
/* 
   A function, takes inputs in their parameters, and these are called arguments 
   and depending on what your function does, they return a value also known
   as output.  
*/


// This function has only 1 parameter of type String, and it returns a String
// but it has no tuple and it does not need one. Lets create one.  

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

    return greeting
}
    // we want this function to return TWO values, which is a tuple

                              // This is the tuple
func greeting(person: String) -> (String, String) {
    let language = English
    let greeting = Hello \(person)"

       // and we need to return the values as a tuple
    return (greeting, language)
}

Now we want to give the returns a name so that we know what we are going to return.

                          // we just modify the tuple, so the returns have names
func greeting(person: String) -> (greeting:String, language: String) {
    let language = "English"
    let greeting = "Hello \(person)"

    return (greeting, language) // this returns both constants
}

var result = greeting("Tom") // Here we give the function an argument
println(result.language) // this will print “English"

Hope this clears it a bit.. let me know if you don’t understand something.