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

Vikram Babu
Vikram Babu
2,158 Points

The code I wrote returns an error even though there is no errors when pasted into the playground. Why?

The code attached is the code I copied into playground, an no errors resulted. Does anyone know why this is the case? Does anyone have suggestions as to what I can do to fix the errors in my code?

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

    return greeting
}

var result = greeting(person: "Tom")

swift var result = greeting(person: "Tom") needs to be part of a function. Basically, any line of code should be in scope. So if you put the line of code in a function, it should run. For example:

func greeting(#person: String) -> (greeting: String, language: String) {
    let greeting = ("Hello \(person) ", "in English!")

    return greeting
}
func foo()
{
     var result = greeting(person: "Tom")
}

All you need to do now is to run the foo() function.

1 Answer

Stepan Ulyanin
Stepan Ulyanin
11,318 Points

Try making the greeting a tuple of variables you want to return and return the whole tuple

func greeting(#person: String) -> (greeting: String, language: String) {
    let greeting = "Hello \(person) "
    let language = "in English"

    return (greeting, language)
}

var result = greeting(person: "Tom")