In this tutorial, we will learn about Function in Kotlin. A function is a small piece(block) of code which can perform some special task and it only executes when we call this function. The function allows us to reuse code. In Kotlin we use the fun keyword to declare a function.
Page Contents
fun functionName(parameterName: parameterType, parameterName: parameterType): returnType {
//body of function
}
It is important to note, you must have to mention data type of each parameter at the time of defining a function.
Please check out the following example program to understand how to define and call a function in Kotlin.
Example Program:
//defining function
fun addition(num1: Int, num2: Int): Int{
return num1+num2
}
fun main (args: Array<String>){
var ans=addition(10,20) //calling the function
println("Ans = "+ans)
}
Output:
Ans = 30
In case you have a very small piece of code in function body then you can also mention return type of function using assignment operator(=). please check out the following example.
Example program:
//defining function in single line
fun addition(num1: Int, num2: Int = 0) = num1+num2 //return value using =
fun main (args: Array<String>){
var ans=addition(10,20) //calling the function
println("Ans = "+ans)
}
Output:
Ans = 30
Function Default Arguments
In Kotlin, while defining a function we can specify the default value of parameters. The default value will be used when the corresponding argument is omitted from the function call. Check out the following example.
Example Program:
//defining function with default value parameter
fun addition(num1: Int, num2: Int = 0) = num1+num2 //return value using =
fun main (args: Array<String>){
var ans=addition(10,20) //calling the function
println("Ans = "+ans)
ans = addition(20) //Second paramter will use defualt value
println("Ans = "+ans)
}
Output:
Ans = 30
Ans = 20
The function named arguments:
In Kotlin we can specify the name of arguments while passing it to function. This makes function class so clean and readable. Check out this example program.
//defining function with default value parameter
fun sum(num1: Int, num2: Int = 0, num3:Int=0) = num1*num2+num3 //return value using =
fun main (args: Array<String>){
var ans=sum(10,5,2) //simply calling the function
println("Ans = "+ans)
ans = sum(num1=20,num3=3) //calling the function with named arguments
println("Ans = "+ans)
}
Output:
Ans = 52
Ans = 3
It is important to note, Mixing named and positioned arguments are not allowed in Kotlin.
Varargs(Variable number of arguments):
In Kotlin, you can pass a variable number of arguments while defining a function. You just have to add a vararg parameter in the function definition. At the call of this type of function, you can pass any number of parameters. Check out the following example.
//defining function with vararg parameter
fun sum(vararg numbers: Int): Int {
var sum: Int = 0
for(number in numbers) {
sum += number
}
return sum
}
fun main (args: Array<String>){
var ans=sum(10,5,2,25,10,15) //sending any number of variable
println("Ans = "+ans)
ans=sum(16,15,9,10,16,25,21,33) //sending any number of variable
println("Ans = "+ans)
}
Output:
Ans = 67
Ans = 145
Spread operator:
In case you have an array and you want to send an array to a function which has vararg parameter defined in it. You can do this with the help of the spread operator(*). Check out the following example.
//defining function with vararg parameter
fun sum(vararg numbers: Int): Int {
var sum: Int = 0
for(number in numbers) {
sum += number
}
return sum
}
fun main (args: Array<String>){
val arr = intArrayOf(10,5,2,25,10,15)
var ans=sum(*arr) //sending array using spread operator
println("Ans = "+ans)
}
Output:
Ans = 67