Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Functions Function Return Types

Anders Lund
Anders Lund
3,061 Points

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

Ryan Jin
Ryan Jin
15,337 Points

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 :)

Anders Lund
Anders Lund
3,061 Points

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

Ryan Jin
Ryan Jin
15,337 Points

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