-1

I declared a function, for example,

func test(a: Int, b: Int)

but when I invoke it, i have to put a code like this:

test(12, b: 32)

It looks so weird !

I declared the test function with symmetrical parameters, but the function call is not symmetric.

How can I declare the function to make the function call exactly like this:

test(12, 32)

Microos
  • 1,728
  • 3
  • 17
  • 34
  • 1
    Take a look at this answer : http://stackoverflow.com/questions/24045890/why-does-a-function-call-require-the-parameter-name-in-swift . – iamyogish Oct 15 '15 at 12:30
  • 1
    Have a look at ["Function Parameter Names"](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID166) in the Swift book ... it is all documented. – Martin R Oct 15 '15 at 12:32
  • imyogish, Martin R thx, both of u! I seems the # has been canceled in swift2! I get it thx! – Microos Oct 15 '15 at 12:37

2 Answers2

2

The full form of function declarations is:

func test(parameterLabelA parameterNameA: Type, parameterLabelB parameterNameB: Type)

By default if you omit labels as in:

func test(a: Int, b: Int)

the following form is inferred:

func test(_ a: Int, b b: Int)

where _ means no label.

In order to remove the b label you can declare your function as:

func test(a: Int, _ b: Int)

Note however that this is not the recommended style.

Nicholas H.
  • 1,321
  • 13
  • 26
  • 1
    like Nicholas points out, it is not the recommended style, it could lead to confusion. Just rather use: func( test test : Int, test2 test2 : Int) – dirtydanee Oct 15 '15 at 12:35
1

Simple as that:

func test(x : Int, _ y : Int) {
    print(x)
    print(y)
}

Edit: added inner parameter names.

Adam
  • 26,549
  • 8
  • 62
  • 79