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

John Scully
John Scully
1,018 Points

Extra, extra credit?

I put this together as a way of calling multiple functions. What I'm interested in is any feedback to make the code more streamlined as it seems like I'm using a lot of lines:

let salutation = "Hello"

func fullName(firstName: String, lastName: String) -> String {
    return ("\(firstName) \(lastName)")
}

func personalGreeting(fullName: String) ->String {
    return ("\(salutation) \(fullName)")
}

personalGreeting(fullName("Iggy", "Azalea"))

2 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

I think a closure would make fewer lines:

func personalGreeting(salutation: String, fullName: (firstName: String, lastName: String) -> (String)) ->String {
    return ("\(salutation) \(fullName)")
}

personalGreeting(salutation: "Hello", firstName:"Iggy", lastName"Azalea")

See if that works.

John Scully
John Scully
1,018 Points

hmmm I think I can see what you're getting at here but I can't get it to execute. The function's themselves seem to run fine but I haven't quite understood how to correctly call the functions.

I've tried the following:

personalGreeting(salutation: "Hello", firstName:"Iggy", lastName:"Azalea")
personalGreeting("Hello", ("Iggy" , "Azalea"))
personalGreeting("Hello", "Iggy", "Azalea")

I would have done it like this..

func personalGreeting(firstName: String, lastName: String) -> String {
    return ("Hello \(firstName) \(lastName)")
}

personalGreeting("Iggy", "Azalea")
John Scully
John Scully
1,018 Points

yeah i wanted to call multiple functions rather than just rely string interpolation. I figure it'll be important later. thanks though!