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 Functions Function Return Types

Return Statement wrong?

Don't know whats wrong here, i'm trying to return a string from the func. "greeting" but there is something wrong, plz help :)

returns.swift
func greeting(person: String) {
    return "Hello "+ person
}

greeting ("Tom")

3 Answers

You have to specify the return type in the function by using a -> and the return type, so your code should look something like this:

func greeting(person: String) -> String{
    return "Hello "+ person
}

greeting ("Tom")

If you are not returning anything, or in another word, you are returning void, you can either ignore the -> (type) or write -> Void, but if you are returning a type like an int or a string, you have a specify it. Hope that helps you :)

aha, thank u :)

U also have to change this //return "Hello "+ person to //return "Hello (person)" for it to run, atleast in the teamThreeHouse program thing.

Yeh, probably programmed too much Javascript and I forgot this, thanks for reminding me