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 trialJosh Smith
1,049 PointsCan't get my return function right
I've attempted to perform the return function and I've done it wrong as usual! When I try run this code I get the error message that my code took too long to run...
func greeting(person: String) -> String {
return greeting("Hello \(person)")
}
greeting("Tom")
1 Answer
David Magaña
4,170 PointsHi Josh, you have that error because you are calling the greeting function inside the same greeting function, so every time you call that greeting function, you are making an infinite loop. All you have to do is to return the string you already have, like this:
func greeting(person: String) -> String {
return "Hello \(person)"
}
greeting("Tom")
Josh Smith
1,049 PointsJosh Smith
1,049 PointsDammit I was close with one of my other variations... Seems like the story of my life with Swift so far haha... Thanks David I appreciate it!