Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Functions Function Return Types

Josh Smith
Josh Smith
1,049 Points

Can'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...

returns.swift
func greeting(person: String) -> String {
    return greeting("Hello \(person)")
}

greeting("Tom")

1 Answer

Hi 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
Josh Smith
1,049 Points

Dammit 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!