Swift function overloading

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.