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


Return in function

Hello team!

I am afraid I do not completely understand what exactly should I return using return type. Here is an example of func code from your video course:

func carpetCostHaving(length: Int, width: Int, carpetColor color: String = "tan") -> (price: Int, carpetColor: String) { // Gray carpet - $1/sq ft // Tan carpet - $2/sq ft // Deep Blue carpet - $4/sq ft

let areaOfRoom = area(length: length, width: width)

var price = 0

switch color {
case "gray": price = areaOfRoom * 1
case "tan": price = areaOfRoom * 2
case "blue": price = areaOfRoom * 4
default: price = 0

return (price, color)

As you can see we return price and color. But why cannot we return carpetCostHaving or areaOfRoom . When or how should I know what value to return? Sorry for this stupid question, I am just confused a bit ;(

1 Answer

Zeka, it can depend on what your objectives are with the method. In this case, you can interchange the variables and return the savings or the total area by changing the return line near the bottom. If you would like to return all four, you can definitely do that too.

If you have any questions, feel free to ask!