Tsallaka zuwa abun ciki
Justice Tech School

Kotlin Functions

Declare with fun. Parameters need types, and the return type follows a colon. Single expression functions use =. Unit means nothing is returned.

Example
fun add(a: Int, b: Int): Int { return a + b }
fun square(x: Int) = x * x
fun greet(name: String) = println("Hello $name")
println(add(2, 3))
println(square(6))
greet("Ada")

Exercise

Return type position.

fun f():  = 5

Quiz Test

Where does the return type go?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.