-3

I'm studying the Swift code now, and for the func, I have a question.

The code is

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

getMilk(bottles: 4)


  • You want a return value? `func getMilk(bottles: Int) -> Float { ... return cost }`, and `let cost = getMilk(bottles: 4)`? – Larme Jan 31 '22 at 08:55
  • Thank you!! `func getMilk(bottles: Float) { let cost: Float = bottles * 1.5 print("Milk cost is \(cost)") } getMilk(bottles: 2) ` – Andy Woo Jan 31 '22 at 09:11
  • [Swift functions](https://docs.swift.org/swift-book/LanguageGuide/Functions.html) – Joakim Danielson Jan 31 '22 at 09:27
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 09 '22 at 11:53

1 Answers1

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

This example will return the total volume of milt in bottles, given the number of bottles and each bottle's volume.

Great job on deciding to learn Swift!

Please remember not to use magic numbers in your code.

DmitryoN
  • 310
  • 2
  • 11