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

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

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

You are welcome, glad you did !