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

Tyler Proctor
Tyler Proctor
11,458 Points

Does a tuple's return type need to be defined in the function's declaration if both data types are a string?

I have watched the videos on tuples multiple times and I am still confused by this. In the video he uses two different types of data (Boolean and a String), so it makes sense that he must define both data types seperately in the declaration of the function. However, in the first objective, both data types are Strings. Do you need to define the return type as two different Strings in order for it to be considered a tuple? I am also confused as to why the name of the function in this objective and a constant variable within the function are both named "greeting." Is there a purpose for doing that, and I am missing it? I feel that naming the same exact thing would cause some complications. I have tried this code in many ways by now and I am not sure if I am failing because of conceptual errors or small syntactical errors.

2 Answers

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,716 Points

I didn't go back and watch the video, but I've noticed that often the challenges are deliberately a different example case than the video to reinforce the concept in a different way.

Tuples can be any data type, but if you are returning a tuple, you need to specify the data types when you specify the return value.

Think of the function 'greeting' as being the verb/action and the return value 'greeting' as the noun/object. The greeting object is the output from the greeting action. It's a little confusing to have them named the same, but they could have called the function "greet" or "greetingAction" and called the return string "greetingPhrase".

The tricky part is that they want you to name each item in the tuple as well when you specify the return value

-> (greeting: String, language: String)

It feels a bit redundant. The reason for this is that the variable names inside the function ("greeting" and "language") are only within the scope of the function. The function only returns the data, not the name of the temporary variable we were holding that data in. So when we name the items in the tuple in the return value, we're giving that data its name on the outside, which happens to be the same name we gave the data when it was inside the function. Let me know if this makes sense.

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

    return (greeting, language)
}
Tyler Proctor
Tyler Proctor
11,458 Points

Thank you for the response! I did actually get the question correct just before you posted this, but your response did help clarify things a bit. Much appreciated!