Calling the Kotlin function 

Now create two functions in Kotlin, add and addAndReturn. The add function takes two integer variables, adds them, and prints them on the screen, while addAndReturn adds two values and returns the result:

fun add(a : Int, b : Int) {
println("Result of $a + $b is ${a+b}")
}


fun addAndReturn(i: Int, j: Int): Int {
return i + j
}

We can call each Kotlin function by using the filename as a reference. CallKotlin.kt is a file that contains a Kotlin function. When calling a Kotlin function in the Java file, it is important to remember that we must add the kt keyword with the Kotlin filename to call our desired function. For example, we can call the add function by using CallKotlinKt.add. See the following example of the Java file:

public static void main(String args[]) {

CallKotlinKt.add(5,5);

int result = CallKotlinKt.addAndReturn(5,5);
System.out.print("From Kotlin: result = " + result);

}

Execute this code and it will display the following output:

Result of 5 + 5 is 10
From Kotlin: result = 10
..................Content has been hidden....................

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