Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Return issues in Swift

This doesn't work for me, all kinds of weird things happening. I have:

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

When I call this with fullName I get this weirdness.

fullName(firstName: String, lastName: String)

Except firstName: String are editable and only String for the second part is??? What is going on.

I should be able to have this:

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

print(fullName("Phil","Hanson"))

But this doesn't work.

1 Answer

Iman Mk
Iman Mk
6,223 Points

Hi Phil,

All methods called on an object use parameter names except for the first parameter. So your print function would be:

print(fullName("Phil", lastName: "Hanson"))

Also you can have external parameter names to use everywhere if you like to:

func fullName( first firstName: String,  second lastName: String) -> String {
     return firstName + lastName
}
print(fullName(first: "Phil", second: "Hanson"))

Let me know if you have any questions!

Jeez, I did it all right but because I didn't know first parameter didn't use a name, I was freaking out. Works perfect now :(

That seems like a weird design...

Iman Mk
Iman Mk
6,223 Points

Awesome! It is for a reason to work better with obj-c and for another factor. btw for the answer that helped you the best there is an upvote button that lets the commenter know his/her answer was helpful :)