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!

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: Error in returning string as a datatype for extra credit assignment 1.

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

When the above function is called to a variable and then displayed there are no issues. But when the function is called within println() as: println("My name is (fulsome("John","Doe"))")

Xcode does not read my string parameters correctly. says "Expected ',' separator". Dont understand whats gone wrong here.

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Gaurav,

I am not sure what you are willing to do here. In particular I haven't understood what that "fulsome" is.. Is it just a typo?

I have checked the extra credit task to see what it asks and as far as you wrote those first 2 lines of code (which you did), you should be fine.

Trying to guess what you wanted to do I would go, for example with this:

println("My name is " + fullName("John","Doe"))

But I am not sure this is what you what to do as I can't understand that "fulsome" and also please note that both here and in the fullName function code you are missing a space between the first name and the last name.

Vittorio

Hello Vittorio,

Thank you for your advice and time. "fulsome" was a typo(autocorrect). Your solution worked and I am able to run the code. But heres what I am curious about, how does a string behave when returned from a custom function. for example:

For the function below the area is being calculated and can be returned within the println function: func calculateArea(height: Int, width: Int) ->Int { return height * width } println("Area = (calculateArea(10,20))")

Below we have the full name function returning a string func fullName(firstName: String, lastName: String) ->String { return firstName + lastName }

When called to use by the following method(as per your suggestion) there is no problem: var name = fullName("Gaurav", "Raghuvanshy") println(name) println("Name" + fullName("Gaurav", "Raghuvanshy"))

But when treated as a implicit(I guess thats what its called) function like calculateArea above we have errors. println("Name (fullName("Gaurav","Raghuvanshy"))")

Xcode does not recognise the strings as it should and gives the error: "Expected ',' separator" Why is this?

Heres the function and the call I'm curious about: func fullName(firstName: String, lastName: String) ->String { return firstName + lastName } println("Name (fullName("Gaurav","Raghuvanshy"))")