繁体   English   中英

如何使用 func (Swift) 获取牛奶成本

[英]How can I get the milk cost using func (Swift)

我现在正在研究 Swift 代码,对于函数,我有一个问题。

代码是

func getMilk(bottles: Int) {
    var cost = bottles * 1.5
    print("Milk cost is $\(cost)")
}

getMilk(bottles: 4)


    func getMilk(numberOfBottles: Int,
                 bottleVolume: Float) -> Float {
        var cost = Float(numberOfBottles) * bottleVolume
        return cost
    }
    print("Milk func return: \(getMilk(bottles: 5, bottleVolume: 1.5))")

这个例子将返回瓶子中 milt 的总体积,给定瓶子的数量和每个瓶子的体积。

决定学习 Swift 真是太棒了!

请记住不要在代码中使用幻数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM