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 trial

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!