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 trialbecky hayes
2,226 PointsNot sure how to assign a variable to a tuple
Having real problems here ...i've created a variable... but im not sure how to assign a variable to a tuple
func greeting(person: String) -> (greeting: String,language: String) {
let language = "English"
let greeting = "Hello \(person)"
var result = (
return (language, greeting)
}
3 Answers
jonlunsford
16,746 PointsBecky: You are on the right track. Here's how I would implement the function.
func greeting(person: String) -> (language: String, greeting: String) {
let language: String = "English"
let greeting: String = "Hello \(person)"
return (language, greeting)
}
Then you can create a variable called myGreeting and store the return value from the function. In this case a tuple. Using tuple notation you can print the greeting using myGreeting.greeting. To print the language use myGreeting.language. Here's an example...
var myGreeting = greeting("becky")
print(myGreeting.greeting)
becky hayes
2,226 PointsThanks for this! I still cant get past the challenge.. its asking me to:
Create a variable named result and assign it the tuple returned from function greeting. (Note: pass the string "Tom" to the greeting function.)
currently my code looks like this but i dont know what i'm doing wrong!
func greeting(person: String) -> (language: String,greeting: String, result: String) { let language = "English" let greeting = "Hello (person)" var result = greeting("becky")
return (language, greeting, result)
}
jonlunsford
16,746 PointsOutside your function you would declare the variable result. Not in the return statement as you have it above.
func greeting () {
...
}
var result = greeting("your name here")
You are assigning what is returned from the function to a new variable named result. By calling the function, the return value will be stored to result. I hope this helps!