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
Mike NZ
2,415 PointsSwift Function Extra Credit help
The extra credit question says:
"Extra Credit Write a function called fullName which takes two String parameters: firstName and lastName. It concatenates the two parameters and returns them as the fullName."
My attempt:
func fullName(firstName: String, lastName: String) {
return firstName + lastName
}
fullName(John, Smith)
I'm guessing it's obvious, but I get errors all over the show. It doesn't like my attempt at concatenating it with the + (Even though I thought I was doing it the same way as explained here under "Concatenating Strings and Characters" -- https://developer.apple.com/library/ios/documentation/swift/conceptual/swift_programming_language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-XID_428 )
Following on from this, another question, can I have in the return line:
return ("Hello \(firstName) \(lastName)")
or is this only specifically for the println() function?
Thanks!
10 Answers
Meg Cusack
11,448 PointsMike NZ- This is my complete code, which looks just like yours. It is working fine for me so I think it is the correct answer.
func fullName (firstName: String, lastName: String) ->String {
return firstName + lastName
}
fullName("John", " Smith")
Meg
Ian Brennan
1,516 PointsWhen you declare your function, you need to declare a return type. So:
func fullName(firstName: String, lastName: String) {
becomes:
func fullName(firstName: String, lastName: String) -> String {
This defines the return type as String, and ensures your concatenation will work. (I can't remember if it explains this later on, or on the video your on now)
When calling the function, you should pass your params in as String type as well, so:
fullName("John", "Smith")
Mike NZ
2,415 PointsHi Ian,
I've done what you said, and I'm still getting problems with my code. My code is currently:
func fullName(firstName: String, lastName: String) -> String {
return firstName + lastName
}
fullName("John", "Smith")
Even though no errors appear, nothing appears in the Assistant Editor pane. I've also tried messing around with the println( ) command to display fullName, but I can't get it working.
agreatdaytocode
24,757 Points// Write a function called fullName which takes two String parameters: firstName and lastName. It concatenates the two parameters and returns them as the fullName.
func fullName(firstName: String, lastName: String) ->String {
return "\(firstName) \(lastName)"
}
fullName("John", "Smith")
John Stephanites
2,007 PointsThis worked!
agreatdaytocode
24,757 PointsIt should lol :)
Karthikeya Nadendla
9,747 PointsIt did not work for me, it says an error :
Missing argument label 'lastName:' in call
so if it try to fix it by calling the lastName then its working. But why ?
Kenton Hubner
6,314 PointsWhat you are forgetting is to add () inside of quotation marks around firstName and lastName
so like this
func fullName(firstName: String, lastName: String) -> String {
return "\(firstName) " + "\(lastName)"
}
fullName("Kent", "Hubner")
` ` `
kols
27,007 PointsWrite a function called fullName which takes two String parameters: firstName and lastName. It concatenates the two parameters and returns them as the fullName:
func fullName(firstName: String, lastName: String) -> String {
return ("\(firstName) \(lastName)")
}
fullName("Jane", "Smith")
Mike NZ
2,415 PointsAwesome! I didn't know you could do it like that.
Thanks!
Zain Abiddin
2,783 PointsHow do we access Extra Credit questions? I am unable to get on the extra credit page.
Andreaus Perkins
14,402 PointsThe Extra Credit does not open up an editor, if that's what you hoping for. It simply shows the question.
When on a specific course, click the # of # completed drop-down for a section and you'll see the Extra Credit question at the bottom of the pane.
So your path to this Extra Credit question would be: Swift Functions and Optionals > Functions > Extra Credit
Ishan Haque
Courses Plus Student 1,995 PointsThis is fairly simple. There isn't a proper answer here that I found while doing this challenge, I decided to solve it myself.
Basically, you first want to write the function with the two String parameters, 'firstName and lastName'. You also have to declare it will return a string value by the '->' symbol.
func fullName(firstName: String, lastName: String) -> String {}
Then you have to return the values in a way that it would appear like how a normal name would, 'John Hicks' and you can even make it so it returns, 'My name is John Hicks' using interpolation.
return "My name is \(firstName) \(lastName"
Then you would simply return it to a name after the function. The whole code should look like this. ```func fullName(firstName: String, lastName: String) -> String { return "My name is (firstName) (lastName" }
fullName("John", "Hicks")```
JIHOON JUNG
10,927 Pointsfunc fullName(firstName:String, lastName:String) -> String { return "(firstName) (lastName)" }
fullName("Type Your FirstName", "Type Your LastName")
Karthikeya Nadendla
9,747 PointsI wrote the code as follows but it's not working,
func fullName(firstName: String, lastName: String) ->String {
return "\(firstName) \(lastName)"
}
fullName("John", "Smith")
The error is as follows:
Missing argument label 'lastName:' in call
so if i add lastName: then its working. But why ?
damon patron
Courses Plus Student 3,757 PointsThis is my example this is what worked for me.
The challenge said not to have any elements in the Parenthesis so
CHALLENGE ONE
struct Person { let firstName: String let lastName: String
func fullName() -> String { return "(firstName ) (lastName)" } }
CHALLENGE TWO
let aPerson = Person(firstName: "John", lastName: "Smith") let myFullName = aPerson.fullName
Hal Paris
554 PointsHal Paris
554 PointsMike, I've got the code exactly like yours in the second example and it works for me. My only change was to add a space after John so "John " to make the fullName make sense. Though that shouldn't impact your code. I've run into compiler issues occasionally which may be it.
Zain, There's nothing to access, you'll see the extra credit on your track page where it shows you how much you've completed.