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 trialkelley riley
3,159 PointsCan someone tell me why creating a struct and method is helpful, rather than just making a function with parameters?
I understand how to make this method, but it just seems like more work to me. Why is it done this way?
(edited for clarity) Why not just make some function that can do all of the above. Why make a struct, then make a function, then make an instance of that struct, then call the method.
Why not just make a function with parameters for first name and last name, bypassing a struct all together.
struct Person {
let firstName: String
let lastName: String
func fullName() -> String {
return "\(firstName) \(" ") \(lastName)"
}
}
let myName = Person(firstName: "Kelley", lastName: "Riley")
let fullName = myName.fullName()
3 Answers
Jeff McDivitt
23,970 PointsA method is a function??
Jeff McDivitt
23,970 PointsThe challenge is just to assist you in learning structures; although this example is simple and a struct is not needed. So doing it like the below would be fine.
func fullName(firstName: String, lastName: String) -> String {
return "\(firstName) \(lastName)"
}
var myName = fullName(firstName: "Jeff", lastName: "McDivitt")
myName
kelley riley
3,159 PointsCould you explain why a struct would be helpful? It's slowly becoming more clear, but I'm not really understanding why things like structs and classes are helpful in code.
kelley riley
3,159 Pointskelley riley
3,159 PointsSorry if my question isn't clear.
Why not just make some function that can do all of the above. Why make a struct, then make a function, then make an instance of that struct, then call the method.
Why not just make a function with parameters for first name and last name, bypassing a struct all together.
I guess my question should be, what's the point of a struct? It doesn't seem to be changing much, at least in this simple example