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 trialRamiro Aguirre
Courses Plus Student 16,725 PointsTuple Problem
Currently our greeting function only returns a single value. Modify it to return both the greeting and the language as a tuple. Make sure to name each item in the tuple: greeting and language. We will print them out in the next task.
dont know what to do here
it gives you this
func greeting(person: String) -> String { let language = "English" let greeting = "Hello (person)"
return greeting
}
5 Answers
Stone Preston
42,016 Pointsyou need to change the return type of the function to return a tuple instead of a string.
to make a function with a return type of tuple you can put (parameterName: dataType, parameterName: dataType) after the ->.
you can have multiple values inside the tuple, but in this case you just need to add two. greeting, which is a string, and langauge, which is a string as well.
finally you need to return the actual tuple
//change the return type to a tuple
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
//return the tuple
return (greeting, language)
}
Ramiro Aguirre
Courses Plus Student 16,725 Pointsexactly what i was looking for thanks a lot Preston
Marie Nipper
22,638 PointsYou know, of course there are multiple ways of doing it. Stone's example should work just fine. I solved it a little differently. Also, note that I'm new to all this, so I'm going to try and break it down how I understand it. If I'm wrong, someone please let me know.
//this initial line is the function with a single parameter
func greeting(person: String)
//you then tell the function what to return, (denoted by the 'arrow' ->) -- you don't have this in your code you listed --
func greeting(person: String) ->
//we wrap tuple in the paren and add labels. (the labels are the parts before the colon)
//so it's -> (label: returnType, label: returnType)
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
//here's where mine is different.
//A tuple has to be wrapped in paren and separated by a comma to return them in the order they are listed above
let greeting = ("Hello, \(person)", "\(language)")
return greeting
}
Ramiro Aguirre
Courses Plus Student 16,725 Pointsyep i have and clearly i havent get it so far .
i've tried
func greeting(person: String, language: String)
Li Lin
5,007 PointsRamiro, you don't need to modify the
func greeting(person:String, language:String)
The question is asking you to modify what it returns, so your code needs to be modified after the -> symbol.
should look like
func greeting(person: String) -> (greeting: String, language: String)
hope this helps
James Rawls
2,451 PointsThanks for the insight...the error I made was in the return section of the code by trying to call the function within itself. This is what I called <pre><code>return greeting (greeting, language)</code></pre> Should have been <pre><code>return (greeting, language)</code></pre>
Iago Wandalsen Prates
21,699 PointsIago Wandalsen Prates
21,699 PointsHave you seen the tuple video? What have you tried so far?