Function Basics
In this page:
fun functionName(param: Type): ReturnType {
return value
}
fun functionName(param: Type) { // no return value
}
Declaring a Function
A function starts with fun, followed by its name, a parenthesized parameter list, an optional return type, and a body in curly braces.
Example: Declaring a Function
// Define the function `greet` taking `name` and returning a String
fun greet(name: String): String {
// Return `"Hello, $name!"` from this function
return "Hello, $name!"
}
// Entry point: execution of the program starts here
fun main() {
// Print `greet("Kotlin")` to the console, with a trailing newline
println(greet("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
Functions with No Return Value
A function that only performs an action, like printing, and produces no useful value has a return type of Unit, which can be omitted since it is the default.
Example: Functions with No Return Value
// Define the function `printBanner` taking `text`
fun printBanner(text: String) {
// Print "=== $text ===" to the console, with a trailing newline
println("=== $text ===")
}
// Entry point: execution of the program starts here
fun main() {
printBanner("Welcome")
}
Login to try C/C++/Java/PHP code in the editor
Multiple Parameters
A function can take several typed parameters separated by commas, and every one of them (unless it has a default) must be supplied at the call site.
Example: Multiple Parameters
// Define the function `add` taking `a`, `b` and returning an Int
fun add(a: Int, b: Int): Int {
// Return `a + b` from this function
return a + b
}
// Entry point: execution of the program starts here
fun main() {
// Print "Sum: ${add(3, 4)}" to the console, with a trailing newline
println("Sum: ${add(3, 4)}")
}
Login to try C/C++/Java/PHP code in the editor
Calling Functions
Calling a function uses its name followed by parentheses containing arguments in the same order as the parameters were declared.
Example: Calling Functions
// Define the function `multiply` taking `a`, `b` and returning an Int
fun multiply(a: Int, b: Int): Int {
// Return `a * b` from this function
return a * b
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `result`, set to `multiply(6, 7)`
val result = multiply(6, 7)
// Print "6 x 7 = $result" to the console, with a trailing newline
println("6 x 7 = $result")
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to declare a return type when a function does not implicitly return
Unit, causing a compiler error. - Confusing parameter order when calling a function positionally, sending values to the wrong parameters.
- Not realizing a function with no explicit
returnand a block body implicitly returnsUnit, not the last expression's value.
- Functions are declared with
fun name(parameters): ReturnType { body }. - A function with no meaningful return value has (or can omit) a return type of
Unit. - Parameters are typed and, unless given defaults, must all be supplied when calling the function.
- The
returnkeyword sends a value back from a block-bodied function.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: