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 Functions and Optionals Functions Function Return Types

Missing argument label XXX in call

I wrote a short line of code:

func area(a:Int,b:Int) -> Int{ return a * b }

print(area(10, 2))

Then, I get the error message:

"error: missing argument label 'b:' in call"

So, I must write the following for the code to compile:

print(area(10, b: 2)

But, when I write the following I get the error again:

ar(a: 10, b: 2)

"error: extraneous argument label 'a:' in call"

Why is this? Why can't I just write:

print(area(10, 2))

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

The first argument is an anonymous by default. The next arguments are not. So if you want them to be anonymous you add an underscore before it:

func area(a:Int, _ b:Int) -> Int{
    return a * b
}

And then you can call it without its label:

area(10, 2)

It seems arbitrary why they do this. But for a lot of functions the first (and maybe only) argument is usually self-explanatory, so if you had to use its label explicitly when calling it, it would be redundant. So this is just how they decided to set things up to make the largest number of programmers happy most of the time, but it's a trade off, and sometimes it just makes you override it and it's annoying.

Thank you! Is it common to use external names? For example, I find it easies to call the function if I have names. For example:

func area(height a: Int, width b: Int) -> Int{ return a * b }

area(height: 10, width: 2)