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
Robert Deegan
2,848 PointsInstance method not working
Why does this return a blank for name?:
struct Person {
let firstName: String
let lastName: String
func getFullName(firstName: String, lastName: String) -> String {
let fullName = "\(firstName)" + " " + "\(lastName)"
return "The person's full name is \(fullName)"
}
}
let person1 = Person(firstName: "Mickey", lastName: "Mouse")
person1.getFullName()
1 Answer
Jhoan Arango
14,575 PointsHello:
So you have a structure with a couple of properties and a function that will return a string. The reason why it's returning "blank" or it's giving you an error, it's because you are implementing a function that has parameters, and you are calling that same function without giving it arguments to fulfill those parameters.
This implementation has several small things you can change.
First thing
// Delete the parameter names
// Before
func getFullName(firstName: String, lastName: String) -> String {
let fullName = "\(firstName)" + " " + "\(lastName)"
return "The person's full name is \(fullName)"
}
// After
func getFullName() -> String {
let fullName = "\(firstName)" + " " + "\(lastName)"
return "The person's full name is \(fullName)"
}
Second thing
// Fix the interpolation
// Before
func getFullName() -> String {
let fullName = "\(firstName)" + " " + "\(lastName)"
return "The person's full name is \(fullName)"
}
// After
func getFullName() -> String {
let fullName = "\(firstName)\( lastName)"
return "The person's full name is \(fullName)"
}
With these changes, we've made the function work, and made it a bit more simple. You may ask.. "Why did we delete the parameter names?"
Well simple.. The struct has the properties the function is going to be using to return the names, therefore you do not have to give it extra information to it.
Once you create an instance of Person, then the function can access those properties and use them as we did.
Hope you understand, if you don't please let me know..
Good luck
Robert Deegan
2,848 Points"Well simple.. The struct has the properties the function is going to be using to return the names, therefore you do not have to give it extra information to it. Once you create an instance of Person, then the function can access those properties and use them as we did." I totally get it. Thanks so much for all the detail.
The interpolation is still a minor problem. I see how you put a space before lastName, yet I still get an output string in which the 2 names appear without a space separating them.
I really appreciate you giving time to this Rob
Abraham Juliot
47,353 PointsAbraham Juliot
47,353 Pointsyour function needs no parameters