Computer Science : Double

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

Example Question #21 : Primitive Data Types

How do we declare a method to return a Double in Swift (iOS)?

Possible Answers:

func method() -> Double {}

method() -> Double {}

func -> Double {}

func method -> Double {}

Correct answer:

func method() -> Double {}


In Swift, all methods must first say what they are. They are functions, so they are prefixed with func. Next, methods must have a name. In this case, we named it method. All methods need to specify parameters, even if there are no parameters. So, method() i.e. no parameters. Finally, we wanted to return a Double. So we set the return type using -> Double. 

Learning Tools by Varsity Tutors