0
点赞
收藏
分享

微信扫一扫

011_swift_函数当做返回值。函数当做参数

菜头粿子园 2023-03-16 阅读 56


函数当做返回值

func stepForward(Input:Int)->Int{
return Input+1;
}
func stepBackwardForward(Input:Int)->Int{
return Input-1;
}
func chooseStepFunction(isBack:Bool)->(Int)->Int{
return isBack ? stepForward : stepBackwardForward;
}

var currentValue=4
let move=chooseStepFunction(isBack: currentValue>6)
move(8)

函数当做参数

func stepForward(Input:Int)->Int{
return Input+1;
}
func stepBackwardForward(Input:Int)->Int{
return Input-1;
}
func getMathResult(function:(Int)->Int,a:Int){
print("Result:\(function(a))")


}
getMathResult(function: stepForward(Input:), a: 123)
getMathResult(function: stepBackwardForward(Input:), a: 456)

无参数无返回值的函数使用

()->()

表示

举报

相关推荐

0 条评论