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 trialPaula Carvajal
1,679 PointsHow to return a function string
I need help with the last challenge on swift functions and optionals. The challenge is: Modify the definition of the function named greeting to return a string and replace the println statement with a return statement. The function should return a greeting. For example, if you pass in a string "Tom" the the return string would be: "Hello Tom".
func greeting(person: String) -> String {
let person = "Tom"
return person
}
println("Hello \(person)")
2 Answers
Rolf Willems
2,249 PointsYou possible missed the purpose of the (person: String)
part. This part makes it possible for you to call the function using greeting()
, and then in between brackets use the 'person' part. This is why you don't need to declare 'person' within your function, as you declare it inside the brackets of greeting()
, which becomes greeting("Tom")
Also, the function needs to return a greeting. At the moment, the only thing you return when your function 'greeting' is called, is 'person', which just is the name. Use the following to return the required string:
return "Hello \(person)"
This will result in your function ending up like this:
func greeting(person: String) -> String {
return "Hello \(person)"
}
The only thing to do from here on is to correctly call your function, which you do by the following:
greeting("Tom")
Note: The parameter person
is specified when calling the function, so not inside the function itself!
Paula Carvajal
1,679 PointsAwesome, thanks Rolf I understand way better now. I appreciate it!
Rolf Willems
2,249 PointsNo problem, happy coding! :D
Takudzwa Andy Tembani
762 PointsTakudzwa Andy Tembani
762 Pointsthanks bro! was completely stuck there