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

fullName function Extra Credit - programming an space into return

Below is the function I created for the extra credit:

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

fullName("Dave", "Lister")

It returns DaveLister.

How can you automatically include a space between firstName and lastName in the return without having to manually type a space when inputing the value for firstName "Dave"?

i.e. Looking for a solution where you do not have to input "Dave " with a manual space after the 'e'.

Thanks.

2 Answers

Walter Somerville
Walter Somerville
20,698 Points

Hi Frank,

One solution would be to return a formatted string, instead of concatenating the first and last names:

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

Thanks Walter - that's what I was looking for.

Sorry for the delay in answering - I literally got side-tracked off Swift and onto Website Design for a special project.

Appreciate your help.

Frank