Kotlin Function
function means use big problems divided by submodule and easily problems solved. It makes reusability of code and makes the program more manageable
There are two types of functions:
- Standard library function
- User-defined function
Standard Library Function
Kotlin Standard library function is built-in library functions
import java.time.LocalDateTime
fun main(args: Array<String>) {
val current = LocalDateTime.now()
println("Current Date and Time is: $current")
}
User-defined Function
A user-defined is created by the user. User-defined function takes the parameter(s), perform an action and return the result of that action as a value.
- fun main(args: Array<String>){
- sum()
- print("code after sum")
- }
- fun sum(){
- var num1 =5
- var num2 = 6
- println("sum = "+(num1+num2))
- }
paremetarize function :
- 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
- }
Comments
Post a Comment