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

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

Jhoan Arango
Jhoan Arango
14,575 Points

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.