The _ is not necessary for function calls. It is just used to indicate that something does not need to have a name. In regards to how you would refer to your function, You would not have to pass any name for the function call. I'm new to swift 2.0.
It is used to replace the function parameter's external label. So when you call the swift function, you do not need to use function parameter 's external label name to specify which parameter this value is assigned to, just pass the parameter in function parameters definition order.
In Swift, the ampersand (&) indicates that a parameter is being passed inout. Consider this example: Code Block. func addVarnish(_ product: inout String) { product += " varnish"
Argument labels are the parameter names that functions take in. When declaring argument labels, there are options to have them ignored or labeled as something different when calling the functions.
The _
is used to define that the parameter is not named
If you have multiple _
it states that you do not need to name the parameters in your function call
func myFunc(name:String, _ age:String){
}
myFunc(“Milo", "I'm a really old wizard")
If you do not use the underscore you would use
myFunc(“Milo”, age: "I'm a really old wizard")
The _
is not necessary for function calls. It is just used to indicate that something does not need to have a name.
In regards to how you would refer to your function, You would not have to pass any name for the function call.
But since you also don’t define a parameter type this seems to me like an invalid example (it at least doesn’t work in Xcode 7 with Swift 2.0)
Edit:
Since Swift 3.0
myFunc(name: “Milo”, age: "I'm a really old wizard")
Should be used
Swift needs a convention for saying what the name of a function is, including not only the function name itself (before the parentheses) but also the external names of the parameters. The convention is that the names are followed by colons. So here's a function declaration (in Swift 2.0):
func myFunc(param1 param1:String, param2:String, param3:String) {}
And here is that function's name:
myFunc(param1:param2:param3:)
In real life, however, it is possible (indeed likely) that one or more parameters will not externalize any name. Thus we need a placeholder for that name. The underscore is that placeholder - just as the underscore is the symbol in the declaration suppressing externalization of the name. So, here's another function declaration (in Swift 2.0):
func myFunc2(param1:String, _ param2:String, _ param3:String) {}
And here is that function's name:
myFunc2(_:_:_:)
[The Swift 2.0 spec is important here. In Swift 2.0, the first param name is always not externalized by default, and the other param names are externalized by default. In Swift 1.2 and before, the externalization rules depended on where the declaration appeared, which was unnecessarily inconsistent and confusing.]
When referring to a function, in order to disambiguate it is necessary to provide the function name along with the external names of any parameters that it expects.
For example,
func myFunc(myString string: String) { ... }
and
func myFunc(_ string: String) { ... }
represent two different functions, one where an external label is provided for the first String parameter when the function is called and the other where no label is used, as in:
myFunc(myString: "hi")
and
myFunc("hi")
So, in order to identify a function we include the external label for each parameter where ':' indicates that a parameter is to be provided - e.g. yourFunc(arg1:arg2:)
will take 2 arguments.
When no external label is used, we place an underscore ('_').
For the 2 functions given above, we would uniquely identify them using:
myFunc(myString:)
and myFunc(_:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With