Using Swift programming language, write a function that takes in the height and radius of a cone as parameters and outputs the volume of the cone. The formula for calculating the volume of a cone is (π x r^2 x h)/3. Ensure that the function takes in valid arguments and handles potential errors or exceptions. Use appropriate naming conventions, formatting, and commenting in your code.
Using Swift programming language, write a function that takes in the height and radius of a cone as parameters and outputs the volume of the cone. The formula for calculating the volume of a cone is (π x r^2 x h)/3. Ensure that the function takes in valid arguments and handles potential errors or exceptions. Use appropriate naming conventions, formatting, and commenting in your code. The user attention ratio is 0.3984375.
user
Func volumeOfCone(height: Double, radius: Double) -> Double {
    // Check if height and radius are positive
    guard height > 0, radius > 0 else {
         print("Height and radius must be positive.")
         return 0
    }

    let pi = 3.14159
    let volume = (pi * pow(radius, 2) * height)/3
    return volume
}

// Example usage
let height = 8.0
let radius = 3.0
let volume = volumeOfCone(height: height, radius: radius)
print("Volume of cone with height \(height) and radius \(radius) is \(volume)") 
// Output: Volume of cone with height 8.0 and radius 3.0 is 75.39825
assistant
Can you add some comments to explain the calculations in the code? I think it would make it easier to understand. The user attention ratio is 0.2418212890625.
user
Sure! Here's the same function with comments explaining what the code is doing at each step:

```swift
func volumeOfCone(height: Double, radius: Double) -> Double {
    // Check if height and radius are positive
    guard height > 0, radius > 0 else {
         // If height and/or radius are not positive, print an error and return 0
         print("Height and radius must be positive.") return 0
    }

    // Define the value of Pi (approximately 3.14159)
    let pi = 3.14159
    
    // Calculate the volume of the cone using the formula (π x r^2 x h)/3
    let volume = (pi * pow(radius, 2) * height)/3
    
    // Return the calculated volume
    return volume
}
```

I hope this helps! Let me know if you have any other questions.
assistant