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 trialTerrell Winston
Courses Plus Student 1,811 Pointsno clue
hellppp
1 Answer
Johanns Gregorian
8,694 PointsHere is the solution (part 1 and 2), but before you paste this in and proceed, let's review:
struct Person {
let firstName: String
let lastName: String
func getFullName() -> String {
return "\(firstName) \(lastName)"
}
}
let aPerson = Person.init(firstName: "John", lastName: "Smith")
let fullName = aPerson.getFullName()
1- Create a function named getFullName that returns full name (firstName + lastName):
func getFullName() -> String {
return "\(firstName) \(lastName)
}
func getFullName() -> String {...}
: This should be self-explanatory at this point. You start to define a function using keyward func. We are asked to call this function getFullName. Our function does not accept any parameters, so we'll just have empty () after our function name (getFullName). We are told that our function will return fullName, which we know is of type String (-> String { ... }
).
Inside our function, we're asked to construct full name, which is first name + last name. We'll use string interpolation to construct and return our full name here:
return "\(firstName) \(lastName)"
Another we could have written this is:
func getFullName() -> String {
let fullName = "\(firstName) \(lastName)"
return fullName
}
Next, we are asked to initialize Person struct, and assign it to a constant called aPerson. Now, we know that our
let aPerson = Person.init(firstName: "John", lastName: "Smith")
To initialize, we called the Person object, we need to call its init(...)
function, and pass in values for both firstName and lastName since they are initialized (given a value).
Finally, we need to called the method we created earlier (getFullname) and assigns its return data (of type String: -> String) to an another constant called fullName; we do this by:
let fullName = aPerson.getFullName()
I hope this is helps, but I highly encourage you to watch the pervious video.