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 trialAkshat Jain
12,819 PointsWhy don't I need to return a stored property from a method?
func move(direction: Direction) {
switch direction {
case .Up: location.y += 1
case .Down: location.y -= 1
case .Right: location.x += 1
case .Left: location.x -= 1
}
}
Why isn't this code something like this:
func move(direction: Direction) -> Point {
switch direction {
case .Up: location.y += 1
case .Down: location.y -= 1
case .Right: location.x += 1
case .Left: location.x -= 1
}
return location
}
1 Answer
Reed Carson
8,306 PointsWe dont need to retrieve a value from the function. The purpose of the function is to modify existing values. We could do it the way you suggested but that would just add extra steps.
If you wanted to display text on a label on the screen, you could write a function that modifies the label directly, or write one that returns a value which is then assigned to the label. Both work, just the latter example requires more steps.
Akshat Jain
12,819 PointsAkshat Jain
12,819 PointsAh, I see.