Introduction
In this blog, we will look into Kotlin functions. We will see how functions are defined and invoked. If you are new to kotlin and don’t know where to start from, you can check out our other article on Getting started with Kotlin.
In Kotlin, there are two types of functions: built-in functions and user-defined functions. We will also learn when and how to use both of these functions, as well as how to call them. In the next section, we will go over kotlin functions in-depth.
Functions
A function is a reusable piece of code that performs a particular task. It can be used to divide the code into smaller pieces and make it more modular. Functions can be called multiple times by executing a simple short command. Functions can be called multiple times by writing the function name followed by parentheses. If that function takes arguments, you can pass the arguments inside the parentheses as you call the function.
We use the fun keyword to declare a function in Kotlin.
// Function to multiply two numbers a and b
fun multiply(a: Int, b: Int) :Int {
return a*b
}
fun main(){
// Calling multiply function to calculate the multiplication of 2 and 8
var res=multiply(2,8)
print("The Multiplication of 2 and 8 is: $res")
}
Output:
The Multiplication of 2 and 8 is: 16
The above function takes two integer arguments (i.e., a and b) and returns their multiplication as the output.
There are two kinds of functions in Kotlin:
- Built-in function
- User-defined function
Let’s discuss both of these in detail:
Built-in Functions
There are various built-in functions in Kotlin that are already specified in the standard library and are ready to use. For example:
- println(): prints the specified message/output to the screen
- rem(): To calculate the remainder of a number when divided by another number.
In the program below, we will use two of the most commonly used built-in functions: print() and println().
fun main() {
println("Hey there!")
print("Welcome to Coding Ninjas!")
}
Output:
Hey there!
Welcome to Coding Ninjas!
In the below program, we will use sqrt() function to calculate the square root of a Double number:
fun main() {
var num=10.2
// Using Built-in sqrt function to calculate the square root of num
var res= Math.sqrt(num)
println("The Square root of ${num} is: ${res}")
}
Output:
The Square root of 10.2 is: 3.1937438845342623
Some more built-in functions and their use:
- sum(): returns the sum of all the elements present in the given array
- readline(): To take standard input
- abs(): returns the absolute value of the given number
- max(a,b): Used to find the maximum of a and b.
- min(a,b): Returns the minimum value out of a and b.
- pow(): To calculate the power of a number raised to another number.
User-Defined Functions
A user-defined function is one that is defined by the user. Using the keyword fun, we can define our own function in Kotlin. A user-defined function takes one or more parameters, performs an action, and returns the action's result as a value.
Generally, we define a user-defined function as follows:
fun function_name(a1: dataType, a2: dataType,....., an: dataType) : returnType {
//Function Body
return
}
Description of the above function is as follows:
- function_name: Name of the function
- a1: dataType: an argument a1 having datatype as “dataType”. Here dataType can be Int, double, etc.
- returnType: Specifies the dataType of the returned value.
Examples
A function to check if a number is even or not.
// Program to check if a number is even or not
fun isEven(a: Int) :Boolean {
return a%2==0 // If a%2 is equal to 0, it is a even number
}
fun main(){
var num=2
// Calling isEven function to check if num is even or not.
var res=isEven(num)
print("Is $num Even? : $res")
}
Output:
Is 2 Even? : true
Here the function isEven takes ‘a’ as an argument and returns a Boolean value.
A basic divide function which takes two arguments:
// Program to perform a basic divide operation
fun divide(a: Int, b: Int) :Int {
return a/b
}
fun main(){
print(divide(10,2)) // Calling divide function to calculate the division of 10 by 2
}
Output:
5
Here the divide function takes a and b as arguments and returns an integer value.
The return type of function that doesn’t return anything is Unit.
// This function doesn’t return anything. It only prints the input value
fun print_value(a: Int) : Unit{
print("Input given is: ${a}")
}
fun main(){
// Calling print_value function to print the input value
print_value(25)
}
Output:
Input given is: 25
Here the above print_function doesn’t return anything. As a result, the returnType is Unit.
Note: The use of Unit as the return type is optional. If the function doesn't return anything, you may leave the term Unit. Here’s an example for the same:
// return type can be omitted in case of non-returning function
fun print_value(a: Int) {
print("Input given is: ${a}")
}
fun main(){
// Calling print_value function to print the input value
print_value(25)
}
Output:
Input given is: 25




