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 Swift 2.0 Functions Function Parameters Default Values

Paul Je
Paul Je
4,435 Points

Curious

// Deep Blue Carpet - $4/sq ft.

let area = calculateArea(length, width: width)

var price: Int

switch carpetColor {
case "gray": price = a

For this code from the video, for the 2nd line of code, why do you only have to mention length as one value to calculate in the function, whereas the width value is a colon to describe itself again? It's confusing to me.. was hoping somebody can explain why its not consistent. Thanks!

1 Answer

Nikki Bearman
Nikki Bearman
3,389 Points

Hi Paul,

The function calculateArea we're calling is the function you created earlier with the previous videos. Within said function we did not give an external parameter to the first parameter, which was the length parameter. Here is the code I have for calculateArea, just to show you the absence of the external parameter:

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

This means that when we initially call on the calculateArea function in the new carpetCostCalculator function, before entering the values, we are presented with:

let area = calculateArea(length: Int, width: Int)

We've then typed in "length" to replace "length: Int" and then typed "width", but only to replace the "Int", rather than "width: Int". As we've learned from previous videos the first parameter does not display the name, hence you're then left with just length, width: width.

If you did want it to appear more consistent then you could go back and give the initial calculateArea code an external parameter name of 'length' so then when calling this function into the calculateCarpetArea function the code, once the 'length' and 'width' values are entered, would then display as:

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

Hope that's of some help.

Nikki

ps. I know this is a long time after you asked the question so may be a bit late, but thought it might be of some help to others who were wondering the same nonetheless