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

Yuqian Ci
Yuqian Ci
1,586 Points

why this is wrong..

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

greeting("Tom")

Here's my code and it functions in my xcode but it's wrong here. Anyone knows the correct answer?

returns.swift
func greeting(person: String) -> String {
    println("Hello \(person)")
    return person
}

greeting("Tom")

3 Answers

Ryan Jin
Ryan Jin
15,337 Points

I believe that the challenge asks you to return the greeting string, println only prints the greeting string to the console, it doesn't return the value, so you want to return the string that you printed to the console, your code should look like this

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

greeting("Tom")
Yuqian Ci
Yuqian Ci
1,586 Points

Thanks a lot! I guess I didn't get what "return" means exactly.

Ryan Jin
Ryan Jin
15,337 Points

A function does a set of action, and each time you might get a different result. So the return statement basically gives something back and you can assign that value to a variable or a constant, so return ("Hello (person") returns a string, and you can assign this returned string to a variable like

var greetingStatement: String = greeting("Tom")

And the greetingStatement variable will have the value of "Hello Tom"

Yuqian Ci
Yuqian Ci
1,586 Points

I got what you meant. But if I delete the return like this:

func greeting(person: String) { println("Hello (person)") } greeting("Tom")

I can still get "Hello Tom" right? It's just I can't assign it to a variable? But why I need to assign it to a variable if all I want is to print it out? I can also change different person: String to print out different names.

Also what do you mean by "A function does a set of action, and each time you might get a different result."? Could you gimme an example?

Ryan Jin
Ryan Jin
15,337 Points

If you want to print the greeting string, then yes, your way is more efficient when you only print out the string. However, you might want to use the function for something else. For example, you want to combine a bunch of those greeting strings together, but println just basically prints it out and doesn't give you the greeting, so you want the function to return the greeting and use that string to do something else.

Yuqian Ci
Yuqian Ci
1,586 Points

that makes sense! I guess I'll understand it deeper as I learn more into the course. Thank you so much!