Kotlin – Function

Function is a group of inter related block of code which performs a specific task. Function is used to break a program into different sub module. It makes reusability of code and makes program more manageable.In Kotlin, functions are declared using fun keyword. There are two types of functions depending on whether it is available in standard library or defined by user.

  • Standard library function
  • User defined function

Standard Library Function

Kotlin Standard library function is built-in library functions which are implicitly present in library and available for use.

For example.

fun main(args: Array<String>){
    var number = 25
    var result = Math.sqrt(number.toDouble())
    print("Square root of $number is $result")
}

Output :

Square root of 25 is 5.0
  • Here, sqrt() is a library function which returns square root of a number (Double value).
  • print() library function which prints a message to standard output stream.

User defined Function

User defined function is a function which is created by user. User defined function takes the parameter(s), perform an action and return the result of that action as a value.

Kotlin functions are declared using the fun keyword. For example:

fun functionName(){
// body of function
}

We have to call the function to run codes inside the body of the function.

functionName()

Kotlin simple function example :

fun main(args: Array<String>){
    sum()
    print("code after sum")
}
fun sum(){
    var num1 =5
    var num2 = 6
    println("sum = "+(num1+num2))
}

output:

sum = 11
code after sum

Kotlin Parameterize Function and Return Value

Functions are also takes parameter as arguments and return value. Kotlin functions are defined using Pascal notation, i.e. name:type (name of parameter and its type). Parameters in function are separated using commas.If a function does not returns any value than its return type is Unit. It is optional to specify the return type of function definition which does not returns any value.

fun functionName(number1: Int, number2: Int){
.. .. ..
}
.. .. ..
functionName(value1, value2)
.. .. ..

Kotlin parameterize function example

fun main(args: Array<String>){
   val result = sum(5, 6)
    print(result)
}
fun sum(number1: Int, number2:Int): Int{
    val add = number1+number2
    return add
}

Output :

11

Example: Add Two Numbers Using Function :

fun addNumbers(n1: Double, n2: Double): Int {
    val sum = n1 + n2
    val sumInteger = sum.toInt()
    return sumInteger
}
fun main(args: Array<String>) {
    val number1 = 12.2
    val number2 = 3.4
    val result: Int
    result = addNumbers(number1, number2)
    println("result = $result")
}

Output :

result = 15

How functions with arguments and return value work?

Here, two arguments number1 and number2 of type Double are passed to the addNumbers() function during function call. These arguments are called actual arguments.

result = addNumbers(number1, number2)

The parameters n1 and n2 accepts the passed arguments (in the function definition). These arguments are called formal arguments (or parameters).

In Kotlin, arguments are separated using commas. Also, the type of the formal argument must be explicitly typed.

Note that, the data type of actual and formal arguments should match, i.e., the data type of first actual argument should match the type of first formal argument. Similarly, the type of second actual argument must match the type of second formal argument and so on.

is the return statement. This code terminates the addNumbers() function, and control of the program jumps to the main() function.

In the program, sumInteger is returned from addNumbers() function. This value is assigned to the variable result.

  1. both sumInteger and result are of type Int.
  2. the return type of the function is specified in the function definition.
// return type is Int
fun addNumbers(n1: Double, n2: Double): Int {
    ... .. ...
}

If the function doesn’t return any value, its return type is Unit. It is optional to specify the return type in the function definition if the return type is Unit.

I hope you found what you were looking for from this tutorial. If you want more Kotlin tutorials like this, then do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂

Leave a Comment

Your email address will not be published.