Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
In Kotlin, functions are essential building blocks of your code. Functions in Kotlin are declared using the fun
keyword and have a rich set of features. Here's a tutorial to help you understand and create functions in Kotlin:
fun greet() { println("Hello, Kotlin!") } fun main() { greet() // Call the function }
You can define function parameters in parentheses after the function name:
fun greet(name: String) { println("Hello, $name!") } fun main() { greet("Alice") // Outputs: Hello, Alice! }
To specify the return type of a function, you use a colon (:
) after the parameter list:
fun add(a: Int, b: Int): Int { return a + b } fun main() { val sum = add(3, 4) // sum will be 7 println(sum) }
If your function consists of just a single expression, you can use the =
symbol to make it more concise:
fun add(a: Int, b: Int) = a + b
You can provide default values for function parameters:
fun greet(name: String = "World") { println("Hello, $name!") } fun main() { greet() // Outputs: Hello, World! greet("Alice") // Outputs: Hello, Alice! }
You can also use named arguments to specify values for specific parameters, which is useful when a function has many parameters:
fun displayDetails(name: String, age: Int, isStudent: Boolean) { // Function body } fun main() { displayDetails(name = "Alice", age = 20, isStudent = true) }
To allow a function to accept a variable number of arguments of the same type, use the vararg
modifier:
fun printNumbers(vararg numbers: Int) { for (num in numbers) { print("$num ") } } fun main() { printNumbers(1, 2, 3, 4, 5) // Outputs: 1 2 3 4 5 }
You can add new functions to existing classes without modifying their source code:
fun String.shout() = this.toUpperCase() + "!!!" fun main() { println("hello".shout()) // Outputs: HELLO!!! }
These are member functions or extension functions with a single parameter. They can be called without the dot (.
) and parentheses:
infix fun Int.multiplyBy(factor: Int) = this * factor fun main() { val result = 3 multiplyBy 4 // equivalent to 3.multiplyBy(4) println(result) // Outputs: 12 }
You can define functions inside functions:
fun outerFunction() { println("Inside outer function") fun innerFunction() { println("Inside inner function") } innerFunction() }
Functions in Kotlin offer a wide range of flexibility and power, from basics like parameters and return values to advanced features like extension and infix functions. Mastering functions will enable you to write clean, expressive, and modular Kotlin code.
Defining functions in Kotlin:
fun greet(name: String): String { return "Hello, $name!" }
Parameters and return types in Kotlin functions:
fun add(a: Int, b: Int): Int { return a + b }
Default values in Kotlin function parameters:
fun greet(name: String, greeting: String = "Hello"): String { return "$greeting, $name!" }
Named parameters in Kotlin functions:
val result = greet(name = "Alice", greeting = "Hi")
Function overloading in Kotlin:
fun add(a: Int, b: Int): Int { return a + b } fun add(a: Double, b: Double): Double { return a + b }
Lambda expressions in Kotlin functions:
val sum: (Int, Int) -> Int = { a, b -> a + b }
Higher-order functions in Kotlin:
fun operate(a: Int, b: Int, operation: (Int, Int) -> Int): Int { return operation(a, b) }
Recursion in Kotlin functions:
fun factorial(n: Int): Int { return if (n == 0 || n == 1) 1 else n * factorial(n - 1) }
Extension functions in Kotlin:
fun String.toTitleCase(): String { return this.split(" ").joinToString(" ") { it.capitalize() } }
Local functions in Kotlin:
fun calculateArea(radius: Double): Double { fun square(value: Double): Double = value * value return Math.PI * square(radius) }
Function references in Kotlin:
::
syntax.val square: (Int) -> Int = ::squareFunction
Functions with vararg parameters in Kotlin:
fun printNumbers(vararg numbers: Int) { for (number in numbers) { println(number) } }