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

tuple help

in the Following code can someone please explain what is the line,

var result = greeting("Tom")

doing exactly, from what i understand is creating a variable named result and...

very confused to what is really doing.

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

return (greeting, language)

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

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Rosemary Guzman

For clarification purposes I changed the name of the constant greeting inside the function to salutation. This way you are not confused with the name of the function, and the constant.

Here you are creating a function, which the name is greeting, it takes one parameter of type String, and you named the parameter person. Inside the function, you see 2 constants, one called salutation, and the other one language, and they both have default values. When you call the function, to do the task you designed it for, you call it like this nameOfFunction(parameter). So in this case it should look like this greeting(“Rosemary”). Notice how inside the parenthesis we placed your name in quotes (“”). That’s because we specified it to be of type String. 11

So var result = greeting(“Tom”) is calling the function, and immediately adding the value you specify in the parameter (“Tom”) to the variable result. But what happens inside the function, is that Tom goes into the constant salutation since your parameter person is being used locally within the function.

          // the parameter person is used locally, to assign the value to “salutation” with the addition of the string “Hello"
func greeting(person: String ) -> (salutation: String, language: String) {
    let language = English 
    let salutation = "Hello \(person)” // salutation has the value of “Hello Tom”. 

    return (greeting, language)
}



var result = greeting("Tom) 
println (result.salutation)  // prints “Hello Tom"

Hope you understand it this way, if you need more help let me know, and I will try to explain better.

thank you i get it now. Very clear explanation

Jhoan Arango
Jhoan Arango
14,575 Points

You are welcome, glad you did !