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 trialJames Holland
874 PointsAnother iOS Function question
I am having trouble on second challenge question (see below for context):
//Challenge Task 1 of 2: Declare a function named greeting and within that function you will have a println statement that outputs "Hello".
//Challenge Task 2 of 2: Call the function named greeting that you declared in the previous task.
I've tried:
func greeting(person: String) { let person = "Brad" println("Hello (person)") }
Here's the error message:
Bummer! Your greeting
function printed "Hello Brad" when called with "XYZZY" as the parameter. It should have printed "Hello XYZZY".
What am I missing? Thanks!
3 Answers
kjvswift93
13,515 PointsChallenge 2 should look like this:
func greeting(person: String) {
println("Hello \(person)")
}
Then, to call the greeting function and pass in "Tom" as the person for challenge 3 it should look like this:
func greeting(person: String) {
println("Hello \(person)")
}
greeting("Tom")
kjvswift93
13,515 PointsThe challenge isn't asking for any parameters. It should look like this:
func greeting() {
println("Hello")
}
greeting()
James Holland
874 PointsMy mistake, I posted the wrong Challenge Question. Should read:
Challenge Task 2 of 3
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).
John Stephanites
2,007 PointsI had written this function but I didn't have the line breaks as shown so the editor would not compile. I say this because although the code is correct if you do not break it down as Kyle shows it will not compile. What I mean by this is if you put the curly brace on the same line as greeting("Tom") and not on a separate line it will not compile. At least that is how it seems to me. Which is good because it teaches you to write for better readability. It pissed me off at first but I am slowly coming round!
James Holland
874 PointsJames Holland
874 PointsThat did it, thanks Kyle! Not sure why I insisted on adding a Constant!