1, the concept of function overloading
Overloading of functions means that multiple functions have different parameters or return value types that have the same name, and they become overloaded relationships with each other.
2. Different from other languages
The parameter type of the SWIFT function contains the parameter list type and the return value type, for example (double,double)->double is comprised of 2 double type argument lists and a double type of return value type, that is, the function name in Swift is the same, A function with different parameter lists or different return value types can be overloaded, whereas in C + + or Java, the overload of a function is only related to the parameter list and is not related to the return value
3. Example
Func receive (I:int) {
println ("Accept parameters of an int type \ (i)")
}
Func receive (d:double) {
println ("Accept parameters of a double type \ (i)")
}
Func receive (X:int,y:int) {
println ("accepts parameters of two int type x=\ (x), y=\ (y)")
}
Func receive (I:int)-int{
println ("Accept parameter of type int \ (i), return value int type")
return I * I
}
Let A1:int = Receive (10)
Let A2: () = Receive (10)
Let a3:void = Receive (10)
Let A4 = Receive (10.0)
Let A5: () = Receive (10,20)
can also be overloaded when external parameter names are different
The sample code is as follows:
Func receive (X:int, y:int) {
println (receives a parameter of type int: x=\ (x), y=\ (y))
}
Func receive (X X:int, Y y:int) {
println (receives a parameter of type int: x=\ (x), y=\ (y))
}
Let A1:() = Receive (10,20)
Let A2:() = Receive (X:10,Y:20)
This article is from the "Ordinary Road" blog, please be sure to keep this source http://linjohn.blog.51cto.com/1026193/1620370
Swift function overloading