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 trialMichael Ashby
512 PointsHow do I define a function
func greeting(person: String) -> String { println("Hello (person)") } greeting(Tom)
It states that I need to define a 'fun' named 'greeting' that takes one 'String' parameter
2 Answers
Gareth Borcherds
9,372 PointsYou need to return a value in your function because you are saying that you are going to return a value by using the -> String part of the function. This code would compile
func greeting(person: String) -> String {
println("Hello \(person)")
return person
}
greeting("Tom")
Also, you'll need to add the \ that I added before person to get it to print out the variable. Also, if you don't want to return a value you can just delete the -> String part like this
func greeting(person: String){
println("Hello \(person)")
}
greeting("Tom")
Vittorio Somaschini
33,371 PointsHello Michael.
I have noticed 3 things that needs changing in you code:
1) you missed the "\" before "person" in the string interpolation
2) you forgot to surround Tom with "" when calling the function, it should be greeting("Tom")
3) you put "->String" after the function parameters which I do not think is needed
This would work fine:
func greeting(person: String) {
println("Hello \(person)")
}
greeting("Tom")
Michael Ashby
512 Pointsthank you
Michael Ashby
512 PointsMichael Ashby
512 Pointsthank you