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

Please Help

I need to create a fucntion named greeting and give it a parameter called "person" so basically the output is the name in the string plus "hello"

func greeting(person: str) { let greeting + string println("Hello") + string }

3 Answers

Logan R
Logan R
22,989 Points

You have a couple things wrong.

func greeting(person: String) {
    // Command
}

When you make a variable a defined object, you use the colon. Here, they are asking for a string, so you need to define it as a string using "String".

Second:

When you concatenate (Add one to another) a string, you need to put the concatenation inside of the println.

println("Can you see " + person + " okay?")

I hope this helps you out some!

Thankyou once again Logan for saving me aha

What now? I need to put the name Tom in place where "person" is. I always get confused with where you put the data bit like this.

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

Logan R
Logan R
22,989 Points

It just wants you to run the function below your code given the input, Tom.

Example:

func apple(size,color) {
    println("The apple is " + size + " and is " + color + ".")
}

apple(giant,red) // This will produce "The apple is giant and is red."

Tried that and it failed saying this "Your greeting printed "HelloTom" when called with "XYZZY" as the parameter. It should have printed "Hello XYZZY"."

This was the code

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

greeting("Tom")

And when i didnt put speech marks on Tom it just said "could not compile"

Logan R
Logan R
22,989 Points

You need a space after hello

println("Hello " + person)

The thing is very particular like that :P

This is the instructions.. what am i missing? I think its the string iterpolation?

"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)."

Code

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

greeting("Tom")
Logan R
Logan R
22,989 Points

You added the greeting("Tom") too soon. Remove it then click "next" then add it back for task 3, lol.

yeah that worked haha. So annoying that was the problem. They should tell you but then i guess if i understood it would probably have been clear. Anyway thanks for the help :)

Here is the answer to that question:

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

But don't just copy and paste it and click next. Understand what it is doing and what is going on here or you have gained nothing and will need further advice in the future. ;-)

This worked for me

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