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 trialRyan Anderson
4,610 PointsHaving a hard time understanding Challenge Task 2 of 3. Could someone please break this down for me? thanks
.
func greeting(person: String) -> ( greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
var result =
3 Answers
Steve Hunter
57,712 PointsBrendan,
You have returned your tuple in the wrong order, I think. That is why your result
variable fails the test for this challenge
The return statement should be return (greeting, language)
Steve.
Steve Hunter
57,712 PointsHi Ryn,
Let's start with the solution to part 2:
var result = greeting("Tom")
So, in the previous part, you amended a method to return a tuple by changing it's method signature (the first bit of the method) and also the method body; the return
statement.
The next part asks you to assign the result of the method into a variable called result
. As above, we declare a variable, call it result
and then use the method that you've amended by passing in the required parameter "Tom"
. This method returns a tuple so that is now stored in the variable result
.
I hope that makes sense!
Steve.
Brendan O'Brien
5,521 PointsHi Steve:
Tried that but it fails:
Error message: Bummer! Your result
variable has the wrong value in it. Check the task instructions again.
These are the instructions: Create a variable named result and assign it the tuple returned from function greeting. (Note: pass the string "Tom" to the greeting function.)
func greeting(person: String) -> (greeting:String, language:String) {
let language = "English"
let greeting = "Hello \(person)"
return (language, greeting)
}
var result = greeting("Tom")
Brendan O'Brien
5,521 PointsBoy did I tear my hair out over that for a couple hours...
Steve, you were absolutely correct. Re-ordering the return statement did the trick.
On a more general note, does the order matter in real code?
This worked:
func greeting(person: String) -> (greeting:String, language:String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
var result = greeting("Tom")
Steve Hunter
57,712 PointsAbsolutely the order matters in real code. Your tuple could contain the results of two separate calculations, say, holding the speed and direction of a particle. If you were expecting the speed to be contained in tuple.0
and the direction to be found in tuple.1
but these two results were reversed, your calculation is likely to be incorrect.
Similarly, in our little example in this code challenge, if your application is expecting to discover the user's language by accessing result.1
but instead found that contained "Hello Tom"
, that's likely to cause unforseen results.