Reputation: 627
Ok, so im starting to learn programming using swift and I know more than the basics but I can't figure this out. So if I'm trying to make a function with some stablished constant values and some variables it seems like I can't define constants without them acting like variables. For example, here I want to make a function for the Area of a Circle:
func CircleArea(radius: Int) {
println(radius*radius*3.1415926)
}
I also tried
struct Circle {
let pi = 3.1415926
var radius: Int
func CircleArea(pi: Int, radius: Int) {
println(radius*radius*pi)
}
}
Upvotes: 1
Views: 61
Reputation: 236260
You can't multiply Int times Double. You need to convert your Int to Double first:
println( Double(radius*radius)*3.1415926 )
func circleArea(radius: Int) -> Double {
return Double(radius*radius) * M_PI
}
Note: You should name your methods starting with a lowercase letter. For the mathematical pi value you can use M_PI (Double)
Upvotes: 1