In this tutorial you will learn about the Kotlin function and its application with practical example.
Kotlin Function
Kotlin functions are one of the important part of any kotlin program. In Kotlin, function gives you way to wrap up the set of statements that means to perform any specific task and give it a name, so that it can be invoked later from any where in the program.
Functions makes it easy to divide the complete program into sub-units that perform a specific task for that program, this way it enhance the modular approach and increase the code re-usability of the program.
We pass information in function call as its parameter and the function can either returns some value to the point it where it called from or returns nothing.
Advantages of function
- It enhance the modularity of the program.
- It enhance the re usability.
- It makes development easy as development can be shared in team.
- It reduces the coupling.
- It reduces duplication.
Type Of Function
- Built in function
- User defined functions
Kotlin Built In Function
Built in functions are the functions implicitly available in Kotlin Programming Language to perform some common and standard tasks that includes the functions for file access, mathematical computations, graphics, memory management etc.
Example:-
1 2 3 4 5 |
fun main(args: Array<String>) { var num= 9.0 var res= Math.sqrt(num) print("Square Root of $num is $res") } |
Here,
sqrt() is a built-in function that returns the square root of given number (Double value).
print() built-in function function which prints a given string to standard output stream.
Output:-
1 |
Square Root of 9.0 is 3.0 |
Kotlin User defined functions
User defined function are custom function defined by user it self to perform as custom task that is not available as built in, in this way user can define and write subprograms as functions to perform a task relevant to their programs.
Defining Function In Kotlin
A function must be defined prior to use, you can define a function following the syntax given below –
Syntax:-
1 2 3 |
fun <function_name>(){ // function body } |
Later, you can invoke or call this function as following –
1 |
<function_name>() |
Example:-
A simple function to add two numbers –
1 2 3 4 5 6 7 8 |
fun main(args: Array<String>){ addnum() } fun addnum(){ var num1 =10 var num2 =5 println("Sum of $num1 and $num2 is: "+(num1+num2)) } |
Output:-
1 |
Sum of 10 and 5 is: 15 |
Kotlin Function with parameter and return value
Like any other programming languages Kotlin functions can also takes parameter as arguments and can return value.
Syntax:-
1 2 3 |
fun <function_name>(<param1_name: param1_type,param2_name: param2_type...>){ // function body } |
Later, you can invoke or call this function as following –
1 |
<function_name>(arg1,arg2...) |
Example:-
1 2 3 4 5 6 7 8 |
fun main(args: Array<String>){ val result = addnum(20, 10) print("Sum of given numbers is: $result") } fun addnum(num1: Int, num2:Int): Int{ val res = num1+num2 return res } |
Output:-
1 |
Sum of given numbers is: 30 |