1

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)
  }
}       
1
  • There is a predefined constant M_PI, so you don't have to define your own. Commented Apr 19, 2015 at 9:53

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! i hadn't notice that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.