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

Can you guys help me with this Return Type Function.

Thanks for help.

 //This code works:

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


let name = fullName("Jose Paulo ","Garcia")
println("Hello \(name)")

// or

fullName("Jose Paulo ","Garcia")



// This code does not work:

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



println("Hello \(fullName("Jose Paulo ", "Garcia"))")     //<----- expected separator

3 Answers

Stone Preston
Stone Preston
42,016 Points

the multiple quotations are probably what are causing the problem. the compiler sees the first double quote in front of hello, then gets to the second double quote inside the function call and thinks thats one string.

you can try to use one type (single or double) for the outer quotes, and the other for the inner quotes:

println("Hello \(fullName('Jose Paulo ', 'Garcia'))")

I used double quotes for the outer quotations of the println string, and single quotes for the string parameters.

im not certain that will work though, as swift might require string to be enclosed in double quotes.

I tried that one too but it didn't work. Thank you though.

Your comment regarding strings needing to be enclosed with double quotes is correct. The only way around this that I've found is setting the function to some object or using string concatenation.

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

println("Hello " + fullName("Roger ", "Barry"))

Awesome it did work! Thanks a lot guys! Appreciate. Now I can finally sleep well tonight.

Chris Hedstrom
Chris Hedstrom
3,024 Points

Thanks, I had exactly the same question and was setting it up the same way.

If you don't want to print the "Hello" you can -

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

println(fullName("Roger","Barry"))