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 Syntax and Parameters

Nik Parekh
Nik Parekh
598 Points

Modify the println statement within the greeting function to use the variable person. For example, if you pass a person

func greeting (person : String) { let greeting = "Hello, " + person + "!"

} println(Hello("Tom"))

This is what i have got so far

4 Answers

Holt Hunter
Holt Hunter
4,629 Points

You don't need to make a constant to store your greeting in. The quiz said to make the function print out the greeting:

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

Also you can easily use string interpolation by adding a backslash followed by parentheses with the variable or constant's name. println("Hello /(person)") This prints: "Hello Name". The code above should work for the quiz because it uses string interpolation and it takes an input person and prints: "Hello Name".

Hope this helps!

Arman Kussainov
Arman Kussainov
4,754 Points

You can write like this: println("Hello, /(person) !")

McKenna Rowe
McKenna Rowe
2,930 Points

I'm getting a weird error for this. Please see: http://imgur.com/kfP7Gck

Why does it think I've entered "XYZZX"? Isn't the code correct as well overall?

Holt Hunter
Holt Hunter
4,629 Points

Hi McKenna,

Your problem is that is hasn't asked you to call the greeting function until task 3, so your answer for task 2 should look like this:

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

Not this:

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

greeting("Tom")

Hope this helps!

McKenna Rowe
McKenna Rowe
2,930 Points

Doh! Got it. The error msg doesn't really make sense though. I think I keep having issues somehow with my browser, trying to go back a step or something?

Brandon Weis
Brandon Weis
1,757 Points

I was experiencing some trouble. Thanks for the clarification!