Functions are essential for any programming language. Kotlin functions to organize a code group so the developer can reuse the code. In today’s tutorial, we will learn how to define a function in Kotlin, Call a function, and use a function in Kotlin.
Kotlin Functions: Defining, Usage & Examples
What is a function in Kotlin?
A function is a code block that performs a specific task. Programming code inside a function is defined once and can be reused multiple times.
Syntax of function in Kotlin:
1 2 3 4 5 |
fun functionName(parameters): ReturnType { // body of the function } |
In Kotlin we use fun keyword to declare a function.
Example of a simple function in Kotlin:
1 2 3 4 5 6 7 8 9 10 |
fun testHello() { return println("Hello, world!") } fun main() { testHello() } // Output Hello, world! |
Functions with parameters in Kotlin:
In parameter functions, we can pass parameters while defining a function and pass values when calling a function to perform a certain task on those values.
1 2 3 4 5 6 7 8 9 10 11 12 |
fun printUserName(name: String) { println("Hello, $name!") } fun main() { printUserName("Kotlin Guide") } // Output Hello, Kotlin Guide! |
Functions with parameters and return a value in Kotlin:
we will perform a SUM of two numbers and return the result. The number is passed using parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fun sum(x: Int, y: Int): Int { return x + y } fun main() { println(sum(10, 7)) } //Output 17 |
You can also store its returning value in a variable.
1 |
val result = sum(10, 7) |
Functions with Named parameters arguments in Kotlin:
Named arguments make it easy to understand our function and we can pass the values in any manner. There is no need to follow the arguments in order. We just have to define the argument name while calling a function and assign its value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fun demoPrintUserName(firstName: String, lastName: String) { println("$firstName $lastName") } fun main() { // Uses named arguments with swapped parameter order demoPrintUserName(lastName = "Guide", firstName = "Kotlin") } // Output Kotlin Guide |
Functions with Default parameter values in Kotlin:
In Kotlin we can define a default value to the function parameter. If the user does not pass the parameter value on function calling time than the default value will be assigned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun printUserName(name: String = "KotlinGuide") { println("Hello, $name!") } fun main() { printUserName() // Output = Hello, KotlinGuide! printUserName("James Bond") // Output = Hello, James Bond! } |
Single expression function in Kotlin:
A single expression function returns a single line of expression. To assign a single expression to the function we use the = operator.
1 2 3 4 5 6 7 8 9 |
fun subtract(a: Int, b: Int) = a - b fun main() { print(subtract(50, 30)) // Output = 20 } |
Lambda expression in Kotlin:
In Kotlin we can write shorter code using lambda expression. In this example, we will be converting a String to upper case using lambda expression so we don’t have to create an external function to perform this task.
1 2 3 4 5 6 7 |
fun main() { val upperCaseString = { text: String -> text.uppercase() } println(upperCaseString("kotlinguide")) // Output = KOTLINGUIDE } |
Extension functions in Kotlin:
In Kotlin we can extend existing classes to a function. For example, we can extend a String reverse function to our user-defined function.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun String.reverse(): String { return this.reversed() } fun main() { val text = "KotlinGuide" println(text.reverse()) // Output: ediuGniltoK } |
Extension function to check whether a number is Even or Not.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun Int.isEven(): Boolean { return this % 2 == 0 } fun main() { val number = 10 println(number.isEven()) // Output: true } |
Conclusion:
Always use meaningful names for your function so you will remember them at the time of use. Always make each function dedicated to a single task only this way you can manage large levels of code in small code snipped. Keep practicing functions in Kotlin and you will master them, Happy coding.