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 trialMatt Frerichs
4,015 PointsWhen adding a method to a class, why do I not have to identify the Type that the result should return?
I completed the challenge for adding the method to the Button class. However, when I initially created the method, I was declaring the Type that the function should return. Here is the first revision:
// func incrementBy (points: Double) -> Double { width = self.width + points height = self.height + points }
//
Why do you not have to include '-> Double' in the method? Up until now I have declared the Type that the function needs to return.
class Button {
var width: Double
var height: Double
init(width:Double, height:Double){
self.width = width
self.height = height
}
func incrementBy(points: Double) {
width = self.width + points
height = self.height + points
}
}
1 Answer
Joshua Rapoport
8,415 PointsYou don't have to add the return typeat the end of the function declaration, mainly because within that function, you don't have to return anything. If you're using a function to edit of value, and unless you are actually "return"ing A double value, there's no need for "-> Type" In this exercise, you're editing the values of two properties, and while you are returning a new value to those properties, the function itself is returning no value at all.
Matt Frerichs
4,015 PointsMatt Frerichs
4,015 PointsThanks for the help, that clears things up for me.