Functions in Kotlin or any other programming language are lines of code clubbed together under a name
Example
fun addNumbers()
{
var x = 4
var y = 5
var z = x+y
}
Explanation
- Starts with the keyword “fun” followed by a name
- The name usually suggests the functionality, what the lines of code do
- In our example we have the logic for adding two numbers accordingly we have named it as addNumbers()
Let’s start with the one we have been using while learning the basics
I. Main Function
After setting up IntelliJ IDE for coding in Kotlin
We created a file
And then…..
Did we randomly put lines of code into the file?
No. We added a function called as “main” and then put the lines of code into that.
Why main function?
Because “main” marks the entry point into the application. Tells the compiler where the execution starts from.
What is kotlin standard library?
Function that are commonly used while working in kotlin are defined in Kotlin standard library.
for example – main, print
“main” – when used the compiler knows from where the execution starts
“print” – when used the compiler prints to the console
For the rest part user can define functions as per there requirements.
What are user defined functions?
Functions defined by user’s as per there own individual requirements are called as user-defined functions.
So what are the types of functions in Kotlin?
- Library functions – Commonly used ones defined in a library
- User defined functions – Defined by user as per requirement
Example for Library function
fun main (args:Array<String>)
{
//main function
}
Example for user defined function
Function with logic to add two numbers
fun addNumbers()
{
var x = 4
var y = 5
var z = x+y
}
II. Implementation details
There are two parts to a functions
- Function declaration
- Function call
Declaring a function
- Mentioning the function in the file is called as declaring it
//function declaration
fun addNumbers()
{
var x = 4
var y = 5
var z = x+y
}
Calling a function
- Just the declaration does nothing, for the function to be executed the method is to be called inside main -> calling a function
fun main (args:Array<String>)
{
addNumbers() // function call
}
The whole code goes something like this
fun main (args:Array<String>)
{
addNumbers() // function call
}
// function declaration
fun addNumbers()
{
var x = 4
var y = 5
var z = x+y
println("Result $z")
}
- Kotlin standard library has declaration for library functions all we have to do is call it.
- User defined functions will have to be declare and then called.
Before wrapping up let us answer the question
Why do we use functions?
“main” marks the entry point into the application and all the code goes into the main method
As the code grows, too much code in the main method becomes hard to read , maintain and check for errors
That is why we organise the code by splitting the logic
This not only makes the code easy to read, maintain and check for errors, it also helps with re usability
Example
Consider we had to perform addition on 3 sets of numbers, without functions our code would look something like this
fun main (args:Array<String>)
{
//adding first set of numbers
var x1 = 10
var y1 = 20
var z1 = x1+y1
//adding second set of numbers
var x2 = 11
var y2 = 22
var z2 = x2+y2
//adding third set of numbers
var x3 = 70
var y3 = 30
var z3 = x3+y3
}
- The code grows in lines making it difficult for read, maintain and test
- Also we are writing the same logic over and over again
So we simplify and organise the code by using functions
fun main (args:Array<String>)
{
//adding first set of numbers
addNumbers(10,20)
//adding second set of numbers
addNumbers(11,22)
//adding third set of numbers
addNumbers(70,30)
}
fun addNumbers(x: Int, y: Int) {
var z = x+y
println("Result $z")
}
- Instead of writing the same logic over and over again, we define it once and reused it with the help of functions
- The simplification as you can see helps us better to read and maintain the code