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

Michael Bates
Michael Bates
13,344 Points

How do I call a name within a function?

How do I call a name within a function? I am getting an error dealing with the name Tom, and I am not quite sure what is wrong.

parameters.swift
func greeting(person: String) {
    println("Hello")
}
greeting (Tom)
println("Hello (\(greeting)")

1 Answer

Hi Michael,

Try:

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

So your func takes a String as a parameter. Strings are contained in double quotes. So, pass in Tom surrounded by double quotes. Inside the func, the output is then "Hello Tom" as we've used String interpolation to include the contents of the variable person inside the string. Make sense?

I hope that helps,

Steve.

Michael Bates
Michael Bates
13,344 Points

Indeed it does make sense now! Thank you for the help, Steve!

No problem; glad you got it sorted. :smile: