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 Object-Oriented Swift Inheritance Overriding Methods

Why do we need to change the discountedPrice function (to returning a Double)? How if we not use a "return" in a "func"

What is the syntax when we use a function without a return?

1 Answer

Matthew Young
Matthew Young
5,133 Points

You're returning a Double because you're dealing with prices, which have decimals. If you want a function that doesn't have a return type, just omit the arrow (i.e. "->") and return type. For example, the following function does not have a return type:

func discountedPrice(percentage: Double) {
     self.price = self.price - (self.price * percentage / 100.0)
}

That function is only modifying the "price" property of our class, which means I don't need to return a value.

When you're calling that function, you just need to call the actual method using dot notation along with the instance name. For example,

tshirt = Clothing("tshirt", price: 49.99)
tshirt.discountedPrice(50.0)

I hope this helps. For more info, you can check this out: Swift Programming Guide: Initialization Specifically, look under the subsection "Initializer Inheritance and Overriding". The document talks about overriding a superclass's init method, but you can pretty much look at it as if you're overriding a superclass's function.