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

I am not sure what is wrong with my code in the last task, part two, of Tuples

It wants me to create a variable named result and passing it the tuple returned from function greeting. Also it needs to pass the string "Tom" to the greeting function. please help!!

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

    return (language, greeting)
}

var result = greeting(person: "Tom")
Woongshik Choi
Woongshik Choi
42,387 Points

Try -------> var result = greeting("Tom")

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hey Marc!

func greeting(person: String)...

Firstly, this function is correct. But you are calling it via

greeting(person: "Tom")

although the first parameter has no explicit name defined, so you would have to call it like this:

greeting("Tom")

If you want to use a named parameter, you can alter your greeting function like this

func greeting(person person:String)...  // first is the parameter name, you can name it anything you want
func greeting(#person: String)...       // shorthand if variable and name should be the same

Secondly, as you are returning a tuple from the greeting function, unless you don't want to assign the tuple but a specific value of it, you can specify which value you want to assign like this

let result = greeting("Tom").greeting // Access via returned variable names
let result = greeting("Tom").1        // Access return values via index (0-based)
let (_, result) = greeting("Tom")     // Assign all return values to constants, _ is a placeholder and will not be assigned

It is usually a good idea to have a playground open in Xcode, there you can test your code as you write. Xcodes' warnings and errors are quite helpful, not even talking about autocompletion ;)

Hope that helps! Happy coding :)