Function with parameters

Functions can receive one or more parameters as arguments:

fun hello(message : String) : Unit {
println("Hello from $message")
}

fun main(args: Array<String>) {
hello("Kotlin")
}

The hello function takes a string variable as a parameter. When the hello function is called from main, a string value is passed to that function.

If the function takes more than one parameter, all parameters are separated with a comma (,). Here is another example of writing a function that takes two parameters:

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

fun
main (args: Array<String>){
add(4,5)
}

In a function declaration, variables cannot be declared with the val or var keywords, and the data type must be specified explicitly.

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

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