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

iOS Intermediate Swift Properties Computed Properties

What's the difference between using struct and function here?

The out come of these two codes are the same so what makes them different and why are we using struct here instead of function? I understand Pasan is just using a simple example to demonstrate, but I am curious to understand the practicality of this..

struct Rectangle { let length: Int let width: Int

var area: Int {
    return length * width
}

}

func area(length length: Int, width width: Int) -> Int { return length * width }

1 Answer

Maria Angelica Dadalt
Maria Angelica Dadalt
6,197 Points

The point of the video wasn't just to calculate the area. It was to show how computed properties work. You use a struct because you are defining an object that can do a bunch of stuff. And you can use a computed property instead of a full blown function because it's a simple calculation. A function can do A LOT more.

I suggest you watch the classes and structs video again for more information and also read the language guide if you don't understand why you should create objects yet.