4.5 Set default values for function parameters [Swift original tutorial]

Original: http://coolketang.com/staticCoding/5a992500fe88c21c80b142f8.html

1. This lesson will show you the default values ​​of functions. You can set a default value for any parameter, and if you define a default value for a parameter, you can omit that parameter's value when calling the function. 



2. First introduce the interface tool framework that needs to be used. 


3. Put parameters with default values ​​at the end of the parameter list, which ensures that all non-default parameters of the calling function use the normal order. Here a default value is set for the second parameter. 


4. Obtain the horizontal axis distance between two coordinates, this function is used to calculate the distance between two points. 


5. Obtain the distance along the vertical axis between the two coordinates. 


6. Finally, at the end of the function, calculate and return the distance between two points. 


7. Define two point coordinate objects in turn, and initialize their horizontal and vertical coordinates. 


8. Call the function to calculate the coordinates between two points. At this point, values ​​are specified for both parameters. 


9. The function is called again, but with only the value of the first parameter set and the second parameter omitted. In this case, the default value of the second parameter will be used to calculate the distance. 


This article is organized from: "Swift4 Interactive Tutorial", the real [Hands-on] teaching mode, get started with iOS development and Swift language at the fastest speed, free download from Apple Store App Store: https://itunes.apple.com/cn/app/id1320746678 , or scan the QR code at the bottom of this page . Course supporting material download address: data download


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325822505&siteId=291194637