Lesson 8 of 17
Functions
Functions in Kotlin
Functions are declared with fun:
fun add(x: Int, y: Int): Int {
return x + y
}
For single-expression functions, you can use the concise = syntax:
fun add(x: Int, y: Int): Int = x + y
fun square(n: Int): Int = n * n
Call functions normally:
println(add(3, 4)) // 7
println(square(5)) // 25
Your Turn
Write a function cube(n: Int): Int that returns n cubed, and a function sumOfCubes(a: Int, b: Int): Int that returns the sum of the cubes of a and b.
JS Transpiler loading...
Loading...
Click "Run" to execute your code.