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 trial

iOS Swift Functions and Optionals Parameters and Tuples Tuples

confused with tuple

Having difficult finishing this task with tuples, please help.

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

    return (greeting, language)
}
var result = return (greeting, language)
let result
Pierre Smith
Pierre Smith
11,842 Points

Your suppose to pass the result of the func greeting to the result variable.

remember the function returns both language and greeting strings. In order to get the return value of a function you call it after the assignment operator of the variable you would like to store it in.

i.e. var result = greetings("Tom")

If you don't know how a function works post a comment and I'll reply.

I know a function returns a value...My issue is the exact syntax, order and arrangement. Quite confused. If i watch the video about 32 times I might figure it out but I need and exact example for the task i am trying to accomplish otherwise I am guessing 42 times and still not getting the right answer

The fact that greeting is the func and used as a constant is driving me stupid

3 Answers

Felix Salazar
Felix Salazar
3,879 Points

You have pretty much everything done, just have to call the function greeting and assing it to the constant result. Lets say:

let constantName = functionName(param)
Thomas Jernbro
Thomas Jernbro
4,459 Points

As Pierre wrote: var result = greeting("Tom") is the correct way to call it, since the return statement can only be called from within the function.

And since result should be a variable you should remove the "let result" line since you don't want it as a constant.

I must admit i was also confused by the same name on the function and the constant. So behind the scenes when the return statement is called it will look something like this: greeting(greeting=Hello Tom, language=English) It is then sent to the result variable. This is not actual code, just trying to give an example. :)

Aris B
Aris B
1,415 Points
func greeting(person: String) -> (greeting: String, language: String) {
    let language = "English"
    let greeting = "Hello \(person)"

    return (greeting, language)
}
var result = greeting("Tom")