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

Swift Functions help!

What is wrong with my code?

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

2 Answers

Dave Berning
Dave Berning
17,365 Points

You're close Jesse. Make sure you state the return type as "String", change "println" to "return", and remove the parenthesis around your return value. You also don't need to create an instance of greeting for this code challenge.

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

Thank you so much! i actually successfully completed this challenge a while ago. But, i still don't understand it.

(person: String)
return "Hello (person)" ---------- What exactly does 'person' return?

Dave Berning
Dave Berning
17,365 Points

Jesse Lopez, "person" is an argument that the "func greeting" accepts. If you create an instance of greeting somewhere in your code, one of the things that you have to do is provide a string; this can be any string (that's where (person: String) -> String comes in).

When I create the instance with a variable (or constant) I'm assigning it to the "greeting" function". The string in the parenthesis of the variable which is sent to the greeting function and returns, "Hello Dave". It's helpful if you want to reference that function later in your code somewhere else. So instead of typing. "Hello Dave" over and over again, I can just call the variable "name" or even reassign it later.

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

var name = greeting("Dave") // an instance of greeting
// outputs: "Hello Dave"

// If you want to repeat "Hello Dave" somewhere else in your code, you can just call the variable.
name // outputs: "Hello Dave"

// or even reassign it!
name = greeting("Jesse") // outputs: "Hello Jesse"

After the -> arrow, define the data type of the return, which is String.

Also, your call says greetin, you're missing the g on the end.