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 Robertson
1,934 PointsReturn Types code challenge.
I have been stuck on this challenge for a while and cant work out why the output is not Hello Tom, even though I have specified Println("hello (greeting)").
The aim of the challenge was to create a return type to display Hello Tom. I have run out of ideas :(
func greeting(person: String) -> String {
return person
}
println("Hello \(greeting)")
3 Answers
rtprjct
30,548 PointsYour function is correct, when you call it, you need to give it the string "Tom"
println("Hello " + greeting("Tom"));
James Robertson
1,934 PointsThank you! :)
James Robertson
1,934 PointsThank you! :)
Mike Atkinson
6,882 PointsYes as artie91 said, you need to pass an argument to the function.
When declaring, the part in the round brackets is the parameter. To call a func, you need to supply it with the required argument(s) to match the parameter(s).
For example here "SomeString"
is the argument to match the parameter (person: String)
.
//declaring with one parameter
func greeting(person: String) {}
//calling with one argument
greeting("SomeString")
a func can have 0,1, or more parameters.
eg.
//Declaring
func newFunc() {
println("Funky")
}
//calling
newFunc()
//prints "Funky"
Highly recommend about functions here, at the apple developer library.
Whitman Huntley
6,811 PointsThis did it for me.
func greeting(person: String) -> String {
let greeting = "Hello \(person)"
return greeting
}
println(greeting("Tom"))
James Robertson
1,934 PointsJames Robertson
1,934 PointsDon't worry, solved it! Didn't need the Println statement after all... this code worked: