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

Maher Alhasan
Maher Alhasan
974 Points

Syntax and Parameters

Modify the println statement within the greeting function to use the variable person. For example, if you pass a person named "Tom" to the function then it should display: Hello Tom. (Hint: you must use string interpolation).

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

greeting("Tom")

What's wrong with my answer?

5 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Maher.

You are asked to use string interpolation here, not concatenation like you did.

So, after "println" you should use only one set of "", something like this:

("Hello (person)")

Also please note that the dot here is not requested in the string you want to print out

;)

Maher Alhasan
Maher Alhasan
974 Points

Thanks Vittorio, but it turned out to another error: func greeting(person: String) { println("Hello (person)") } greeting(Tom)

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

You should NOT have the greeting function call in step 2.

Try deleting greeting("Tom")

Maher Alhasan
Maher Alhasan
974 Points

got them all correct, Vittorio. thanks a lot for your enormous help

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Sorry Maher, I got distracted while typing. The other thing that was needed is to add a backslash before the variable, to have it actually treated as a variable and not a string:

("Hello (person)")

Apologies for that.

Maher Alhasan
Maher Alhasan
974 Points

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

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Are you at step 3/3 ?

When you call the function, the word Tom has to go between "", like this:

greeting("Tom")

Also, I have noticed that I had put the backslash also in my previous answers but the site hides them.

Make sure you have a backslash right before (person).

This should be good then. Let me know

Maher Alhasan
Maher Alhasan
974 Points

i am in step 2 Bummer! Your greeting printed "Hello Tom" when called with "XYZZY" as the parameter. It should have printed "Hello XYZZY".

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

Maher...this should work

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