Search code examples
swiftfunc

swift func: i can't understand underscore as a parameter name that can compile


how to use underscore parameter

func test(currentName name: String, _: Int) {

    print("aa\(name) abc");
    //how to use  _  parameter?
}

test(currentName:"aa", 3)

Solution

  • _ means when you call test function, you don't have to write the text before the second parameter test(currentName:"aa", 3)

    if you declare your function like this:

    func test(currentName name: String, secondParameter: Int) {    
        print("aa\(name) abc");
        //how to use  _  parameter?
    }
    

    then when you call test function, you must to call like this:

    test(currentName:"aa", secondParameter: 3)