Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the swift function

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use the swift function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use the swift function" bar!

Default parameter func greet (_ person: String = "zhang san", _ hometown: String = "bei jing")-> String {return "Hello\ (person)! Glad you could visit from\ (hometown)"} print (greet ()) / / Hello zhang san! The number of Glad you could visit from bei jing indefinite parameters / / the number of indefinite parameters func sum (_ nums: Int...)-> Int {var ret = 0 for n in nums {ret + = n} return ret} print (sum (1meme 2J 3)) / / 6print (sum (1J 2J 3J 4J 5) / / 15 change the parameter value defined outside the function inside the function / / change the parameter value defined outside the function func swap (_ a: inout Int) _ b: inout Int) {let temp = a = b = temp} var a = 1var b = 2swap (& a, & b) print (a) / / 2print (b) / / 1 function type variable func add (_ a: Int, _ b: Int)-> Int {return a + b} var addFunc: (Int, Int)-> Int = addprint (addFunc (1,2)) / 3 function type parameter func printResult (_ f: (Int) Int)-> Int, _ a: Int, _ b: Int) {print ("Result:\ (f (a, b)")} printResult (add, 1Power2) / / Result: 3 function type as the return value func forward (_ input: Int)-> Int {return input + 1} func backward (_ input: Int)-> Int {return input-1} func step (isBack: Bool)-> (Int)-> Int {return isBack? Backward: forward} / / countdown to 0var currValue = 3let moveNearerToZero = step (isBack: currValue > 0) print ("Counting to zero:") while currValue! = 0 {print ("\ (currValue)...") CurrValue = moveNearerToZero (currValue)} print ("zero!") / * Counting to zero: 3. 2... 1... Zero! * / embedded function func chooseStepFunc (backward: Bool)-> (Int)-> Int {func stepForward (input: Int)-> Int {return input + 1} func stepBackward (input: Int)-> Int {return input-1} return backward? StepBackward: stepForward} print ("Counting to zero:") var cVal =-4let toZero = chooseStepFunc (backward: cVal > 0) while cVal! = 0 {print ("\ (cVal)...") CVal = toZero (cVal)} print ("zero!") / * Counting to zero:-4. -3. -2. -1. Zero! * / Thank you for reading, the above is the content of "how to use the swift function". After the study of this article, I believe you have a deeper understanding of how to use the swift function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report