Why does a function call require the parameter name in Swift?

后端 未结 6 1225
夕颜
夕颜 2020-11-27 10:42

I have this Function in a class:

func multiply(factor1:Int, factor2:Int) -> Int{
    return factor1 * factor2
}

I try to call the functi

6条回答
  •  心在旅途
    2020-11-27 10:59

    Because your "multiply" function is a method, and like Objective-c, the parameters in methods are part of the name.

    For example you can do this.

    class Calculator {
    
        func multiply(factor1:Int, factor2:Int) -> Int{
            return factor1 * factor2
        }
    
        func multiply(factor1:Int, factor2:Int, factor3:Int) -> Int{
            return factor1 * factor2 * factor3
        }
    
    }
    

    Here there are two different methods, with different names, multiply(factor2) and multiply(factor2 factor3).

    This rule only apply to methods, if you declare this like a functions outside of a class, then the function call don't require parameter name.

提交回复
热议问题