How to access a function

There are a couple of ways to access a package-level function. One way to do this is by using a package name with each function. This method has already been used in the previous example:

Util.hello()

The second way to access a package-level function is to import each function explicitly by using the import keyword:

import Util.hello
fun main(args: Array<String>) {
hello()
println("Power Function")
println(Util.myPow(5.0,3.0))
}

Notice that by adding import Util.hello, Kotlin allows us to use the hello function without using a package name with it. The third and most common way to import a package is with the wildcard:

import Util.*
fun main(args: Array<String>) {
hello()
println("Power Function")
println(myPow(5.0,3.0))
println("Random number generator")

var range = 5..50
for (i in 1..5) {
println(myRandom(range))
}

println("value of PI is ${PI}" )
println("Area of circle " + areaOfCircle(4.0))
}

This is the most convenient method. By using this approach, functions can be accessed by using a package name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.12.207